/**
 * SQLite Feature Repository Implementation
 *
 * Implements IFeatureRepository using SQLite database.
 * 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/feature.mapper.js';
/**
 * SQLite implementation of IFeatureRepository.
 * Manages Feature persistence with CRUD operations.
 */
let SQLiteFeatureRepository = class SQLiteFeatureRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(feature) {
        const row = toDatabase(feature);
        const stmt = this.db.prepare(`
      INSERT INTO features (
        id, name, slug, description, repository_path, branch,
        lifecycle, messages, plan, related_artifacts,
        agent_run_id, spec_path, created_at, updated_at
      ) VALUES (
        @id, @name, @slug, @description, @repository_path, @branch,
        @lifecycle, @messages, @plan, @related_artifacts,
        @agent_run_id, @spec_path, @created_at, @updated_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM features WHERE id = ?');
        const row = stmt.get(id);
        if (!row) {
            return null;
        }
        return fromDatabase(row);
    }
    async findByIdPrefix(prefix) {
        const stmt = this.db.prepare('SELECT * FROM features WHERE id LIKE ?');
        const rows = stmt.all(`${prefix}%`);
        if (rows.length === 0)
            return null;
        if (rows.length > 1) {
            throw new Error(`Ambiguous ID prefix "${prefix}" matches ${rows.length} features. Use a longer prefix.`);
        }
        return fromDatabase(rows[0]);
    }
    async findBySlug(slug, repositoryPath) {
        const stmt = this.db.prepare('SELECT * FROM features WHERE slug = ? AND repository_path = ?');
        const row = stmt.get(slug, repositoryPath);
        if (!row) {
            return null;
        }
        return fromDatabase(row);
    }
    async list(filters) {
        const conditions = [];
        const params = [];
        if (filters?.repositoryPath) {
            conditions.push('repository_path = ?');
            params.push(filters.repositoryPath);
        }
        if (filters?.lifecycle) {
            conditions.push('lifecycle = ?');
            params.push(filters.lifecycle);
        }
        const where = conditions.length > 0 ? ` WHERE ${conditions.join(' AND ')}` : '';
        const stmt = this.db.prepare(`SELECT * FROM features${where}`);
        const rows = stmt.all(...params);
        return rows.map(fromDatabase);
    }
    async update(feature) {
        const row = toDatabase(feature);
        const stmt = this.db.prepare(`
      UPDATE features SET
        name = @name,
        slug = @slug,
        description = @description,
        repository_path = @repository_path,
        branch = @branch,
        lifecycle = @lifecycle,
        messages = @messages,
        plan = @plan,
        related_artifacts = @related_artifacts,
        agent_run_id = @agent_run_id,
        spec_path = @spec_path,
        updated_at = @updated_at
      WHERE id = @id
    `);
        stmt.run(row);
    }
    async delete(id) {
        const stmt = this.db.prepare('DELETE FROM features WHERE id = ?');
        stmt.run(id);
    }
};
SQLiteFeatureRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteFeatureRepository);
export { SQLiteFeatureRepository };
