-- FTT prefixed tables for unique naming (no collision with other apps).
-- Applied once; use table_prefix in dashboard_config (default ftt_).

CREATE TABLE IF NOT EXISTS ftt_app_admin_config (
  id INT AUTO_INCREMENT PRIMARY KEY,
  config_key VARCHAR(64) NOT NULL,
  config_value LONGTEXT NOT NULL,
  updated_at DATETIME NOT NULL,
  UNIQUE KEY uk_config_key (config_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS ftt_reports (
  id INT AUTO_INCREMENT PRIMARY KEY,
  user_token_hash VARCHAR(64) NOT NULL,
  type VARCHAR(32) NOT NULL,
  title VARCHAR(255) NOT NULL,
  description TEXT,
  admin_response TEXT NULL,
  created_at DATETIME NOT NULL,
  responded_at DATETIME NULL,
  INDEX idx_user_token (user_token_hash),
  INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS ftt_app_logs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  install_uuid VARCHAR(64) NOT NULL DEFAULT '',
  level VARCHAR(16) NOT NULL DEFAULT 'INFO',
  tag VARCHAR(64) NOT NULL DEFAULT 'FTT',
  message TEXT NOT NULL,
  detail TEXT NULL,
  device_info TEXT NULL,
  created_at DATETIME NOT NULL,
  INDEX idx_app_logs_created (created_at),
  INDEX idx_app_logs_install (install_uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS ftt_crash_logs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  install_uuid VARCHAR(64) NOT NULL DEFAULT '',
  error_message TEXT NOT NULL,
  stack_trace LONGTEXT NULL,
  thread_name VARCHAR(128) NULL,
  device_info TEXT NULL,
  created_at DATETIME NOT NULL,
  INDEX idx_crash_logs_created (created_at),
  INDEX idx_crash_logs_install (install_uuid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO ftt_app_admin_config (config_key, config_value, updated_at)
VALUES ('locked_features', '[]', NOW());
