/**
 * SQLite Settings Repository Implementation
 *
 * Implements ISettingsRepository using SQLite database.
 * Enforces singleton pattern (only one Settings record allowed).
 * Uses prepared statements to prevent SQL injection.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { injectable } from 'tsyringe';
import { toDatabase, fromDatabase, } from '../persistence/sqlite/mappers/settings.mapper.js';
/**
 * SQLite implementation of ISettingsRepository.
 * Manages Settings persistence with singleton constraint.
 */
let SQLiteSettingsRepository = class SQLiteSettingsRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    /**
     * Initialize settings for the first time.
     * Creates a new Settings record in the database.
     *
     * @param settings - The settings to initialize with
     * @throws Error if settings already exist (singleton constraint)
     */
    async initialize(settings) {
        // Check if settings already exist (singleton constraint)
        const existing = await this.load();
        if (existing !== null) {
            throw new Error('Settings already exist. Use update() to modify existing settings.');
        }
        // Convert to database format
        const row = toDatabase(settings);
        // Prepare INSERT statement
        const stmt = this.db.prepare(`
      INSERT INTO settings (
        id, created_at, updated_at,
        model_analyze, model_requirements, model_plan, model_implement, model_default,
        user_name, user_email, user_github_username, user_preferred_language,
        env_default_editor, env_shell_preference, env_terminal_preference,
        sys_auto_update, sys_log_level,
        agent_type, agent_auth_method, agent_token,
        notif_in_app_enabled, notif_browser_enabled, notif_desktop_enabled,
        notif_evt_agent_started, notif_evt_phase_completed, notif_evt_waiting_approval,
        notif_evt_agent_completed, notif_evt_agent_failed,
        notif_evt_pr_merged, notif_evt_pr_closed,
        notif_evt_pr_checks_passed, notif_evt_pr_checks_failed,
        notif_evt_pr_blocked, notif_evt_merge_review_ready,
        workflow_open_pr_on_impl_complete,
        ci_max_fix_attempts, ci_watch_timeout_ms, ci_log_max_chars,
        stage_timeout_analyze_ms, stage_timeout_requirements_ms,
        stage_timeout_research_ms, stage_timeout_plan_ms,
        stage_timeout_implement_ms, stage_timeout_merge_ms,
        analyze_repo_timeout_analyze_ms,
        onboarding_complete,
        approval_gate_allow_prd, approval_gate_allow_plan,
        approval_gate_allow_merge, approval_gate_push_on_impl_complete,
        feature_flag_env_deploy, feature_flag_debug,
        feature_flag_react_file_manager,
        feature_flag_projects,
        feature_flag_code_review,
        feature_flag_collaboration,
        feature_flag_bedrock_integration,
        workflow_enable_evidence, workflow_commit_evidence,
        hide_ci_status, default_fast_mode,
        interactive_agent_enabled, interactive_agent_auto_timeout_minutes,
        interactive_agent_max_concurrent_sessions,
        auto_archive_delay_minutes,
        stage_timeout_fast_implement_ms,
        fab_position_swapped
      ) VALUES (
        @id, @created_at, @updated_at,
        @model_analyze, @model_requirements, @model_plan, @model_implement, @model_default,
        @user_name, @user_email, @user_github_username, @user_preferred_language,
        @env_default_editor, @env_shell_preference, @env_terminal_preference,
        @sys_auto_update, @sys_log_level,
        @agent_type, @agent_auth_method, @agent_token,
        @notif_in_app_enabled, @notif_browser_enabled, @notif_desktop_enabled,
        @notif_evt_agent_started, @notif_evt_phase_completed, @notif_evt_waiting_approval,
        @notif_evt_agent_completed, @notif_evt_agent_failed,
        @notif_evt_pr_merged, @notif_evt_pr_closed,
        @notif_evt_pr_checks_passed, @notif_evt_pr_checks_failed,
        @notif_evt_pr_blocked, @notif_evt_merge_review_ready,
        @workflow_open_pr_on_impl_complete,
        @ci_max_fix_attempts, @ci_watch_timeout_ms, @ci_log_max_chars,
        @stage_timeout_analyze_ms, @stage_timeout_requirements_ms,
        @stage_timeout_research_ms, @stage_timeout_plan_ms,
        @stage_timeout_implement_ms, @stage_timeout_merge_ms,
        @analyze_repo_timeout_analyze_ms,
        @onboarding_complete,
        @approval_gate_allow_prd, @approval_gate_allow_plan,
        @approval_gate_allow_merge, @approval_gate_push_on_impl_complete,
        @feature_flag_env_deploy, @feature_flag_debug,
        @feature_flag_react_file_manager,
        @feature_flag_projects,
        @feature_flag_code_review,
        @feature_flag_collaboration,
        @feature_flag_bedrock_integration,
        @workflow_enable_evidence, @workflow_commit_evidence,
        @hide_ci_status, @default_fast_mode,
        @interactive_agent_enabled, @interactive_agent_auto_timeout_minutes,
        @interactive_agent_max_concurrent_sessions,
        @auto_archive_delay_minutes,
        @stage_timeout_fast_implement_ms,
        @fab_position_swapped
      )
    `);
        // Execute with named parameters (safe from SQL injection)
        stmt.run(row);
    }
    /**
     * Load existing settings from the database.
     *
     * @returns The existing Settings or null if not initialized
     */
    async load() {
        // Query the singleton row (table enforces at most one row)
        const stmt = this.db.prepare('SELECT * FROM settings LIMIT 1');
        // Execute query
        const row = stmt.get();
        // Return null if not found
        if (!row) {
            return null;
        }
        // Convert from database format
        return fromDatabase(row);
    }
    /**
     * Update existing settings in the database.
     *
     * @param settings - The updated settings to persist
     * @throws Error if settings don't exist (must initialize first)
     */
    async update(settings) {
        // Check if settings exist
        const existing = await this.load();
        if (existing === null) {
            throw new Error('Settings do not exist. Use initialize() first.');
        }
        // Convert to database format
        const row = toDatabase(settings);
        // Prepare UPDATE statement
        const stmt = this.db.prepare(`
      UPDATE settings SET
        created_at = @created_at,
        updated_at = @updated_at,
        model_analyze = @model_analyze,
        model_requirements = @model_requirements,
        model_plan = @model_plan,
        model_implement = @model_implement,
        model_default = @model_default,
        user_name = @user_name,
        user_email = @user_email,
        user_github_username = @user_github_username,
        user_preferred_language = @user_preferred_language,
        env_default_editor = @env_default_editor,
        env_shell_preference = @env_shell_preference,
        env_terminal_preference = @env_terminal_preference,
        sys_auto_update = @sys_auto_update,
        sys_log_level = @sys_log_level,
        agent_type = @agent_type,
        agent_auth_method = @agent_auth_method,
        agent_token = @agent_token,
        notif_in_app_enabled = @notif_in_app_enabled,
        notif_browser_enabled = @notif_browser_enabled,
        notif_desktop_enabled = @notif_desktop_enabled,
        notif_evt_agent_started = @notif_evt_agent_started,
        notif_evt_phase_completed = @notif_evt_phase_completed,
        notif_evt_waiting_approval = @notif_evt_waiting_approval,
        notif_evt_agent_completed = @notif_evt_agent_completed,
        notif_evt_agent_failed = @notif_evt_agent_failed,
        notif_evt_pr_merged = @notif_evt_pr_merged,
        notif_evt_pr_closed = @notif_evt_pr_closed,
        notif_evt_pr_checks_passed = @notif_evt_pr_checks_passed,
        notif_evt_pr_checks_failed = @notif_evt_pr_checks_failed,
        notif_evt_pr_blocked = @notif_evt_pr_blocked,
        notif_evt_merge_review_ready = @notif_evt_merge_review_ready,
        workflow_open_pr_on_impl_complete = @workflow_open_pr_on_impl_complete,
        ci_max_fix_attempts = @ci_max_fix_attempts,
        ci_watch_timeout_ms = @ci_watch_timeout_ms,
        ci_log_max_chars = @ci_log_max_chars,
        stage_timeout_analyze_ms = @stage_timeout_analyze_ms,
        stage_timeout_requirements_ms = @stage_timeout_requirements_ms,
        stage_timeout_research_ms = @stage_timeout_research_ms,
        stage_timeout_plan_ms = @stage_timeout_plan_ms,
        stage_timeout_implement_ms = @stage_timeout_implement_ms,
        stage_timeout_merge_ms = @stage_timeout_merge_ms,
        analyze_repo_timeout_analyze_ms = @analyze_repo_timeout_analyze_ms,
        onboarding_complete = @onboarding_complete,
        approval_gate_allow_prd = @approval_gate_allow_prd,
        approval_gate_allow_plan = @approval_gate_allow_plan,
        approval_gate_allow_merge = @approval_gate_allow_merge,
        approval_gate_push_on_impl_complete = @approval_gate_push_on_impl_complete,
        feature_flag_env_deploy = @feature_flag_env_deploy,
        feature_flag_debug = @feature_flag_debug,
        feature_flag_react_file_manager = @feature_flag_react_file_manager,
        feature_flag_projects = @feature_flag_projects,
        feature_flag_code_review = @feature_flag_code_review,
        feature_flag_collaboration = @feature_flag_collaboration,
        feature_flag_bedrock_integration = @feature_flag_bedrock_integration,
        workflow_enable_evidence = @workflow_enable_evidence,
        workflow_commit_evidence = @workflow_commit_evidence,
        hide_ci_status = @hide_ci_status,
        default_fast_mode = @default_fast_mode,
        interactive_agent_enabled = @interactive_agent_enabled,
        interactive_agent_auto_timeout_minutes = @interactive_agent_auto_timeout_minutes,
        interactive_agent_max_concurrent_sessions = @interactive_agent_max_concurrent_sessions,
        auto_archive_delay_minutes = @auto_archive_delay_minutes,
        stage_timeout_fast_implement_ms = @stage_timeout_fast_implement_ms,
        fab_position_swapped = @fab_position_swapped
      WHERE id = @id
    `);
        // Execute with named parameters (safe from SQL injection)
        const result = stmt.run(row);
        // Verify update succeeded
        if (result.changes === 0) {
            throw new Error('Failed to update settings: no rows affected');
        }
    }
};
SQLiteSettingsRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteSettingsRepository);
export { SQLiteSettingsRepository };
