/**
 * 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,
        user_name, user_email, user_github_username,
        env_default_editor, env_shell_preference,
        sys_auto_update, sys_log_level,
        agent_type, agent_auth_method, agent_token
      ) VALUES (
        @id, @created_at, @updated_at,
        @model_analyze, @model_requirements, @model_plan, @model_implement,
        @user_name, @user_email, @user_github_username,
        @env_default_editor, @env_shell_preference,
        @sys_auto_update, @sys_log_level,
        @agent_type, @agent_auth_method, @agent_token
      )
    `);
        // 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,
        user_name = @user_name,
        user_email = @user_email,
        user_github_username = @user_github_username,
        env_default_editor = @env_default_editor,
        env_shell_preference = @env_shell_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
      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 };
