/**
 * SQLite IntakeItem Repository Implementation
 *
 * Implements IIntakeItemRepository using better-sqlite3.
 * Manages intake items for the triage workflow.
 */
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/intake-item.mapper.js';
let SQLiteIntakeItemRepository = class SQLiteIntakeItemRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    async create(item) {
        const row = toDatabase(item);
        const stmt = this.db.prepare(`
      INSERT INTO intake_items (
        id, project_id, title, description, source, status,
        triage_notes, suggested_state_id, suggested_priority,
        suggested_labels, suggested_assignee_id,
        resulting_work_item_id, decline_reason, duplicate_of_work_item_id,
        created_at, updated_at, deleted_at
      ) VALUES (
        @id, @project_id, @title, @description, @source, @status,
        @triage_notes, @suggested_state_id, @suggested_priority,
        @suggested_labels, @suggested_assignee_id,
        @resulting_work_item_id, @decline_reason, @duplicate_of_work_item_id,
        @created_at, @updated_at, @deleted_at
      )
    `);
        stmt.run(row);
    }
    async findById(id) {
        const stmt = this.db.prepare('SELECT * FROM intake_items WHERE id = ? AND deleted_at IS NULL');
        const row = stmt.get(id);
        return row ? fromDatabase(row) : null;
    }
    async listByProject(projectId, status) {
        if (status) {
            const stmt = this.db.prepare('SELECT * FROM intake_items WHERE project_id = ? AND status = ? AND deleted_at IS NULL ORDER BY created_at DESC');
            const rows = stmt.all(projectId, status);
            return rows.map(fromDatabase);
        }
        const stmt = this.db.prepare('SELECT * FROM intake_items WHERE project_id = ? AND deleted_at IS NULL ORDER BY created_at DESC');
        const rows = stmt.all(projectId);
        return rows.map(fromDatabase);
    }
    async update(id, fields) {
        const sets = [];
        const params = { id };
        if (fields.status !== undefined) {
            sets.push('status = @status');
            params.status = fields.status;
        }
        if (fields.triageNotes !== undefined) {
            sets.push('triage_notes = @triage_notes');
            params.triage_notes = fields.triageNotes;
        }
        if (fields.suggestedStateId !== undefined) {
            sets.push('suggested_state_id = @suggested_state_id');
            params.suggested_state_id = fields.suggestedStateId;
        }
        if (fields.suggestedPriority !== undefined) {
            sets.push('suggested_priority = @suggested_priority');
            params.suggested_priority = fields.suggestedPriority;
        }
        if (fields.suggestedLabels !== undefined) {
            sets.push('suggested_labels = @suggested_labels');
            params.suggested_labels = fields.suggestedLabels;
        }
        if (fields.suggestedAssigneeId !== undefined) {
            sets.push('suggested_assignee_id = @suggested_assignee_id');
            params.suggested_assignee_id = fields.suggestedAssigneeId;
        }
        if (fields.resultingWorkItemId !== undefined) {
            sets.push('resulting_work_item_id = @resulting_work_item_id');
            params.resulting_work_item_id = fields.resultingWorkItemId;
        }
        if (fields.declineReason !== undefined) {
            sets.push('decline_reason = @decline_reason');
            params.decline_reason = fields.declineReason;
        }
        if (fields.duplicateOfWorkItemId !== undefined) {
            sets.push('duplicate_of_work_item_id = @duplicate_of_work_item_id');
            params.duplicate_of_work_item_id = fields.duplicateOfWorkItemId;
        }
        if (sets.length === 0)
            return;
        sets.push('updated_at = @updated_at');
        params.updated_at = Date.now();
        const stmt = this.db.prepare(`UPDATE intake_items SET ${sets.join(', ')} WHERE id = @id`);
        stmt.run(params);
    }
    async softDelete(id) {
        const now = Date.now();
        const stmt = this.db.prepare('UPDATE intake_items SET deleted_at = ?, updated_at = ? WHERE id = ?');
        stmt.run(now, now, id);
    }
};
SQLiteIntakeItemRepository = __decorate([
    injectable(),
    __metadata("design:paramtypes", [Object])
], SQLiteIntakeItemRepository);
export { SQLiteIntakeItemRepository };
