/**
 * SQLite PmProject Repository Implementation
 *
 * Implements IPmProjectRepository 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/pm-project.mapper.js';
let SQLitePmProjectRepository = class SQLitePmProjectRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(project) {
        const row = toDatabase(project);
        const stmt = this.db.prepare(`
      INSERT INTO pm_projects (
        id, name, slug, description, identifier_prefix, work_item_counter,
        estimate_type, application_id, start_date, end_date, feature_toggles,
        created_at, updated_at, deleted_at
      ) VALUES (
        @id, @name, @slug, @description, @identifier_prefix, @work_item_counter,
        @estimate_type, @application_id, @start_date, @end_date, @feature_toggles,
        @created_at, @updated_at, @deleted_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM pm_projects 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 pm_projects WHERE slug = ? AND deleted_at IS NULL');
        const row = stmt.get(slug);
        return row ? fromDatabase(row) : null;
    }
    async findByIdentifierPrefix(prefix) {
        const stmt = this.db.prepare('SELECT * FROM pm_projects WHERE identifier_prefix = ? AND deleted_at IS NULL');
        const row = stmt.get(prefix);
        return row ? fromDatabase(row) : null;
    }
    async list() {
        const stmt = this.db.prepare('SELECT * FROM pm_projects 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.slug !== undefined) {
            setClauses.push('slug = ?');
            values.push(fields.slug);
        }
        if (fields.description !== undefined) {
            setClauses.push('description = ?');
            values.push(fields.description);
        }
        if (fields.estimateType !== undefined) {
            setClauses.push('estimate_type = ?');
            values.push(fields.estimateType);
        }
        if (fields.startDate !== undefined) {
            setClauses.push('start_date = ?');
            values.push(fields.startDate instanceof Date ? fields.startDate.getTime() : fields.startDate);
        }
        if (fields.endDate !== undefined) {
            setClauses.push('end_date = ?');
            values.push(fields.endDate instanceof Date ? fields.endDate.getTime() : fields.endDate);
        }
        if (fields.featureToggles !== undefined) {
            setClauses.push('feature_toggles = ?');
            values.push(fields.featureToggles);
        }
        values.push(id);
        const stmt = this.db.prepare(`UPDATE pm_projects 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 pm_projects SET deleted_at = ?, updated_at = ? WHERE id = ?');
        stmt.run(now, now, id);
    }
    async incrementWorkItemCounter(projectId) {
        const stmt = this.db.prepare('UPDATE pm_projects SET work_item_counter = work_item_counter + 1, updated_at = ? WHERE id = ? AND deleted_at IS NULL RETURNING work_item_counter');
        const row = stmt.get(Date.now(), projectId);
        if (!row) {
            throw new Error(`Project not found: ${projectId}`);
        }
        return row.work_item_counter;
    }
};
SQLitePmProjectRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLitePmProjectRepository);
export { SQLitePmProjectRepository };
