/**
 * SQLite SdlcTask Repository Implementation
 *
 * Implements ISdlcTaskRepository 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/sdlc-task.mapper.js';
let SQLiteSdlcTaskRepository = class SQLiteSdlcTaskRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(task) {
        const row = toDatabase(task);
        const stmt = this.db.prepare(`
      INSERT INTO sdlc_tasks (
        id, feature_id, task_key, title, description,
        status, sort_order, branch, depends_on_keys, agent_run_id,
        created_at, updated_at
      ) VALUES (
        @id, @feature_id, @task_key, @title, @description,
        @status, @sort_order, @branch, @depends_on_keys, @agent_run_id,
        @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM sdlc_tasks WHERE id = ?');
        const row = stmt.get(id);
        return row ? fromDatabase(row) : null;
    }
    async listByFeature(featureId) {
        const stmt = this.db.prepare('SELECT * FROM sdlc_tasks WHERE feature_id = ? ORDER BY sort_order ASC, created_at ASC');
        const rows = stmt.all(featureId);
        return rows.map(fromDatabase);
    }
    async listAllActive() {
        const stmt = this.db.prepare('SELECT * FROM sdlc_tasks ORDER BY feature_id ASC, sort_order ASC, created_at ASC');
        const rows = stmt.all();
        return rows.map(fromDatabase);
    }
    async upsertByKey(id, featureId, taskKey, fields) {
        const now = Date.now();
        const dependsOnKeysJson = fields.dependsOnKeys && fields.dependsOnKeys.length > 0
            ? JSON.stringify(fields.dependsOnKeys)
            : null;
        const stmt = this.db.prepare(`
      INSERT INTO sdlc_tasks (
        id, feature_id, task_key, title, description,
        status, sort_order, branch, depends_on_keys, agent_run_id,
        created_at, updated_at
      ) VALUES (
        @id, @feature_id, @task_key, @title, @description,
        @status, @sort_order, @branch, @depends_on_keys, @agent_run_id,
        @created_at, @updated_at
      )
      ON CONFLICT(feature_id, task_key) DO UPDATE SET
        title          = excluded.title,
        description    = excluded.description,
        status         = excluded.status,
        sort_order     = excluded.sort_order,
        branch         = excluded.branch,
        depends_on_keys = excluded.depends_on_keys,
        agent_run_id   = excluded.agent_run_id,
        updated_at     = excluded.updated_at
    `);
        stmt.run({
            id,
            feature_id: featureId,
            task_key: taskKey,
            title: fields.title,
            description: fields.description ?? null,
            status: fields.status,
            sort_order: fields.sortOrder,
            branch: fields.branch ?? null,
            depends_on_keys: dependsOnKeysJson,
            agent_run_id: fields.agentRunId ?? null,
            created_at: now,
            updated_at: now,
        });
    }
    async updateStatus(id, status) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE sdlc_tasks SET status = ?, updated_at = ? WHERE id = ?');
        stmt.run(status, now, id);
    }
    async updateSortOrder(id, sortOrder) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE sdlc_tasks SET sort_order = ?, updated_at = ? WHERE id = ?');
        stmt.run(sortOrder, now, id);
    }
    async deleteByFeature(featureId) {
        const stmt = this.db.prepare('DELETE FROM sdlc_tasks WHERE feature_id = ?');
        stmt.run(featureId);
    }
};
SQLiteSdlcTaskRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteSdlcTaskRepository);
export { SQLiteSdlcTaskRepository };
