/**
 * SQLite SdlcSubTask Repository Implementation
 *
 * Implements ISdlcSubTaskRepository 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-subtask.mapper.js';
let SQLiteSdlcSubTaskRepository = class SQLiteSdlcSubTaskRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(subTask) {
        const row = toDatabase(subTask);
        const stmt = this.db.prepare(`
      INSERT INTO sdlc_subtasks (
        id, task_id, feature_id, sub_task_key, name,
        description, status, sort_order,
        created_at, updated_at
      ) VALUES (
        @id, @task_id, @feature_id, @sub_task_key, @name,
        @description, @status, @sort_order,
        @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM sdlc_subtasks WHERE id = ?');
        const row = stmt.get(id);
        return row ? fromDatabase(row) : null;
    }
    async listByTask(taskId) {
        const stmt = this.db.prepare('SELECT * FROM sdlc_subtasks WHERE task_id = ? ORDER BY sort_order ASC, created_at ASC');
        const rows = stmt.all(taskId);
        return rows.map(fromDatabase);
    }
    async listByFeature(featureId) {
        const stmt = this.db.prepare('SELECT * FROM sdlc_subtasks WHERE feature_id = ? ORDER BY sort_order ASC, created_at ASC');
        const rows = stmt.all(featureId);
        return rows.map(fromDatabase);
    }
    async upsertByKey(id, taskId, subTaskKey, fields) {
        const now = Date.now();
        const stmt = this.db.prepare(`
      INSERT INTO sdlc_subtasks (
        id, task_id, feature_id, sub_task_key, name,
        description, status, sort_order,
        created_at, updated_at
      ) VALUES (
        @id, @task_id, @feature_id, @sub_task_key, @name,
        @description, @status, @sort_order,
        @created_at, @updated_at
      )
      ON CONFLICT(task_id, sub_task_key) DO UPDATE SET
        feature_id   = excluded.feature_id,
        name         = excluded.name,
        description  = excluded.description,
        status       = excluded.status,
        sort_order   = excluded.sort_order,
        updated_at   = excluded.updated_at
    `);
        stmt.run({
            id,
            task_id: taskId,
            feature_id: fields.featureId,
            sub_task_key: subTaskKey,
            name: fields.name,
            description: fields.description ?? null,
            status: fields.status,
            sort_order: fields.sortOrder,
            created_at: now,
            updated_at: now,
        });
    }
    async updateStatus(id, status) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE sdlc_subtasks 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_subtasks SET sort_order = ?, updated_at = ? WHERE id = ?');
        stmt.run(sortOrder, now, id);
    }
};
SQLiteSdlcSubTaskRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteSdlcSubTaskRepository);
export { SQLiteSdlcSubTaskRepository };
