USE comptaai_maroc;

CREATE TABLE IF NOT EXISTS system_backups (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  backup_type ENUM('database','uploads') NOT NULL DEFAULT 'database',
  file_name VARCHAR(255) NULL,
  file_path VARCHAR(500) NULL,
  file_size BIGINT UNSIGNED NOT NULL DEFAULT 0,
  status ENUM('created','failed','placeholder') NOT NULL DEFAULT 'created',
  message TEXT NULL,
  created_by BIGINT UNSIGNED NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  KEY system_backups_type_index (backup_type),
  KEY system_backups_status_index (status),
  KEY system_backups_created_index (created_at),
  CONSTRAINT system_backups_created_by_fk FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB;

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

INSERT INTO system_settings (setting_key, setting_value) VALUES
('maintenance.enabled', '0'),
('maintenance.message', 'La plateforme est temporairement en maintenance. Merci de revenir plus tard.')
ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value);
