USE comptaai_maroc;

CREATE TABLE IF NOT EXISTS app_settings (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  setting_key VARCHAR(120) NOT NULL UNIQUE,
  setting_value TEXT NULL,
  is_secret TINYINT(1) NOT NULL DEFAULT 0,
  updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS ai_settings (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  setting_key VARCHAR(120) NOT NULL UNIQUE,
  setting_value TEXT NULL,
  is_secret TINYINT(1) NOT NULL DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

INSERT INTO app_settings (setting_key, setting_value, is_secret) VALUES
('platform.name', 'ComptaAI Maroc', 0),
('platform.default_language', 'fr', 0),
('platform.timezone', 'Africa/Casablanca', 0),
('platform.currency', 'MAD', 0),
('accounting.default_journals', 'AC,VT,BQ,OD', 0),
('accounting.account_clients', '3421', 0),
('accounting.account_suppliers', '4411', 0),
('accounting.account_vat_collected', '4456', 0),
('accounting.account_vat_deductible', '4455', 0),
('accounting.account_purchases', '6111', 0),
('accounting.account_sales', '7111', 0),
('ocr.provider', 'demo', 0),
('ocr.confidence_threshold', '80', 0),
('ocr.auto_send', '0', 0),
('email.smtp_port', '587', 0),
('security.session_timeout', '3600', 0),
('security.password_min_length', '8', 0),
('security.login_attempt_limit', '5', 0)
ON DUPLICATE KEY UPDATE setting_value = setting_value;

INSERT INTO ai_settings (setting_key, setting_value, is_secret) VALUES
('provider', 'openai', 0),
('model', 'gpt-4.1-mini', 0),
('api_key', '', 1),
('enabled', '0', 0)
ON DUPLICATE KEY UPDATE setting_value = setting_value, is_secret = VALUES(is_secret);
