-- FTT config and extra tables: app_news, deployment_history, seed config keys.
-- Config blobs are stored in app_admin_config (unprefixed); loader reads from there.
-- Seed both app_admin_config and ftt_app_admin_config so either schema works.

-- App news (changelog) — unprefixed
CREATE TABLE IF NOT EXISTS app_news (
  id INT AUTO_INCREMENT PRIMARY KEY,
  entry_date DATE NOT NULL,
  title VARCHAR(255) NOT NULL,
  body TEXT NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL,
  INDEX idx_app_news_date (entry_date),
  INDEX idx_app_news_sort (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- App news — prefixed
CREATE TABLE IF NOT EXISTS ftt_app_news (
  id INT AUTO_INCREMENT PRIMARY KEY,
  entry_date DATE NOT NULL,
  title VARCHAR(255) NOT NULL,
  body TEXT NOT NULL,
  sort_order INT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL,
  INDEX idx_ftt_app_news_date (entry_date),
  INDEX idx_ftt_app_news_sort (sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Deployment history — unprefixed
CREATE TABLE IF NOT EXISTS deployment_history (
  id INT AUTO_INCREMENT PRIMARY KEY,
  at DATETIME NOT NULL,
  paths_json TEXT NOT NULL,
  count_val INT NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL,
  INDEX idx_deployment_history_at (at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Deployment history — prefixed
CREATE TABLE IF NOT EXISTS ftt_deployment_history (
  id INT AUTO_INCREMENT PRIMARY KEY,
  at DATETIME NOT NULL,
  paths_json TEXT NOT NULL,
  count_val INT NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL,
  INDEX idx_ftt_deployment_history_at (at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Seed config keys in unprefixed table (config loader reads from app_admin_config only)
INSERT IGNORE INTO app_admin_config (config_key, config_value, updated_at) VALUES
('dashboard_config', '{"uploads_dir":"data/uploads","key_rotation_enabled":true,"previous_key_grace_hours":24,"reset_enabled":false,"reset_token":""}', NOW()),
('gui_settings', '{}', NOW()),
('webhooks', '{"webhooks":[]}', NOW()),
('smtp_config', '{}', NOW()),
('public_db_config', '{}', NOW()),
('crash_ui_config', '{}', NOW()),
('email_config', '{}', NOW());

-- Seed same keys in prefixed table for sites using ftt_ prefix
INSERT IGNORE INTO ftt_app_admin_config (config_key, config_value, updated_at) VALUES
('dashboard_config', '{"uploads_dir":"data/uploads","key_rotation_enabled":true,"previous_key_grace_hours":24,"reset_enabled":false,"reset_token":""}', NOW()),
('gui_settings', '{}', NOW()),
('webhooks', '{"webhooks":[]}', NOW()),
('smtp_config', '{}', NOW()),
('public_db_config', '{}', NOW()),
('crash_ui_config', '{}', NOW()),
('email_config', '{}', NOW());
