/**
 * SQLite Application Repository Implementation
 *
 * Implements IApplicationRepository using better-sqlite3.
 */
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/application.mapper.js';
let SQLiteApplicationRepository = class SQLiteApplicationRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(application) {
        const row = toDatabase(application);
        const stmt = this.db.prepare(`
      INSERT INTO applications (
        id, name, slug, description, repository_path, additional_paths,
        agent_type, model_override, status, setup_complete, agent_session_id,
        git_remote_url, cloud_deployment_provider, cloud_deployment_status,
        cloud_deployment_id, cloud_deployment_url, cloud_deployment_error,
        last_deployed_at, bedrock_enabled,
        created_at, updated_at, deleted_at
      ) VALUES (
        @id, @name, @slug, @description, @repository_path, @additional_paths,
        @agent_type, @model_override, @status, @setup_complete, @agent_session_id,
        @git_remote_url, @cloud_deployment_provider, @cloud_deployment_status,
        @cloud_deployment_id, @cloud_deployment_url, @cloud_deployment_error,
        @last_deployed_at, @bedrock_enabled,
        @created_at, @updated_at, @deleted_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM applications WHERE id = ? AND deleted_at IS NULL');
        const row = stmt.get(id);
        return row ? fromDatabase(row) : null;
    }
    async findBySlug(slug) {
        const stmt = this.db.prepare('SELECT * FROM applications WHERE slug = ? AND deleted_at IS NULL');
        const row = stmt.get(slug);
        return row ? fromDatabase(row) : null;
    }
    async findByPath(path) {
        const stmt = this.db.prepare("SELECT * FROM applications WHERE REPLACE(repository_path, '\\', '/') = ? AND deleted_at IS NULL");
        const row = stmt.get(path.replace(/\\/g, '/'));
        return row ? fromDatabase(row) : null;
    }
    async list() {
        const stmt = this.db.prepare('SELECT * FROM applications WHERE deleted_at IS NULL ORDER BY created_at ASC');
        const rows = stmt.all();
        return rows.map(fromDatabase);
    }
    async update(id, fields) {
        const now = Date.now();
        const setClauses = ['updated_at = ?'];
        const values = [now];
        if (fields.name !== undefined) {
            setClauses.push('name = ?');
            values.push(fields.name);
        }
        if (fields.status !== undefined) {
            setClauses.push('status = ?');
            values.push(fields.status);
        }
        if (fields.additionalPaths !== undefined) {
            setClauses.push('additional_paths = ?');
            values.push(JSON.stringify(fields.additionalPaths));
        }
        if (fields.agentType !== undefined) {
            setClauses.push('agent_type = ?');
            values.push(fields.agentType);
        }
        if (fields.modelOverride !== undefined) {
            setClauses.push('model_override = ?');
            values.push(fields.modelOverride);
        }
        if (fields.setupComplete !== undefined) {
            setClauses.push('setup_complete = ?');
            values.push(fields.setupComplete ? 1 : 0);
        }
        if (fields.agentSessionId !== undefined) {
            setClauses.push('agent_session_id = ?');
            values.push(fields.agentSessionId);
        }
        if (fields.gitRemoteUrl !== undefined) {
            setClauses.push('git_remote_url = ?');
            values.push(fields.gitRemoteUrl);
        }
        if (fields.cloudDeploymentProvider !== undefined) {
            setClauses.push('cloud_deployment_provider = ?');
            values.push(fields.cloudDeploymentProvider);
        }
        if (fields.cloudDeploymentStatus !== undefined) {
            setClauses.push('cloud_deployment_status = ?');
            values.push(fields.cloudDeploymentStatus);
        }
        if (fields.cloudDeploymentId !== undefined) {
            setClauses.push('cloud_deployment_id = ?');
            values.push(fields.cloudDeploymentId);
        }
        if (fields.cloudDeploymentUrl !== undefined) {
            setClauses.push('cloud_deployment_url = ?');
            values.push(fields.cloudDeploymentUrl);
        }
        if (fields.cloudDeploymentError !== undefined) {
            setClauses.push('cloud_deployment_error = ?');
            values.push(fields.cloudDeploymentError);
        }
        if (fields.lastDeployedAt !== undefined) {
            setClauses.push('last_deployed_at = ?');
            values.push(fields.lastDeployedAt instanceof Date
                ? fields.lastDeployedAt.getTime()
                : fields.lastDeployedAt);
        }
        if (fields.bedrockEnabled !== undefined) {
            setClauses.push('bedrock_enabled = ?');
            values.push(fields.bedrockEnabled ? 1 : 0);
        }
        values.push(id);
        const stmt = this.db.prepare(`UPDATE applications SET ${setClauses.join(', ')} WHERE id = ? AND deleted_at IS NULL`);
        stmt.run(...values);
    }
    async softDelete(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE applications SET deleted_at = ?, updated_at = ? WHERE id = ?');
        stmt.run(now, now, id);
    }
    async restore(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE applications SET deleted_at = NULL, updated_at = ? WHERE id = ?');
        stmt.run(now, id);
    }
};
SQLiteApplicationRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteApplicationRepository);
export { SQLiteApplicationRepository };
