import { isRecord } from "../internal/rowMappers.js";
export class ReviewQueueRepository {
    #db;
    constructor(db) {
        this.#db = db;
    }
    upsertReviewQueueEntry(input) {
        const now = new Date().toISOString();
        this.#db
            .prepare(`INSERT INTO review_queue
          (id, workspace_id, run_id, status, risk_tone, headline, summary_label, created_at, updated_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
         ON CONFLICT(id) DO UPDATE SET
           run_id = excluded.run_id,
           status = excluded.status,
           risk_tone = excluded.risk_tone,
           headline = excluded.headline,
           summary_label = excluded.summary_label,
           updated_at = excluded.updated_at`)
            .run(input.id, input.workspaceId, input.runId ?? null, input.status, input.riskTone, input.headline, input.summaryLabel ?? null, now, now);
    }
    listReviewQueue(workspaceId) {
        const rows = this.#db
            .prepare(`SELECT id, workspace_id, run_id, status, risk_tone, headline, summary_label, created_at, updated_at
         FROM review_queue
         WHERE workspace_id = ?
         ORDER BY created_at DESC, id ASC`)
            .all(workspaceId);
        const result = [];
        for (const row of rows) {
            if (!isRecord(row))
                continue;
            const record = {
                id: String(row.id),
                workspaceId: String(row.workspace_id),
                status: row.status,
                riskTone: row.risk_tone,
                headline: String(row.headline),
                createdAt: String(row.created_at),
                updatedAt: String(row.updated_at),
            };
            if (typeof row.run_id === "string")
                record.runId = row.run_id;
            if (typeof row.summary_label === "string")
                record.summaryLabel = row.summary_label;
            result.push(record);
        }
        return result;
    }
    insertReviewDecision(input) {
        const now = new Date().toISOString();
        this.#db
            .prepare(`INSERT OR IGNORE INTO review_decisions
          (id, workspace_id, review_id, decision, reason_code, note_label, decided_at, created_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?)`)
            .run(input.id, input.workspaceId, input.reviewId, input.decision, input.reasonCode ?? null, input.noteLabel ?? null, input.decidedAt, now);
    }
    listReviewDecisions(workspaceId) {
        const rows = this.#db
            .prepare(`SELECT id, workspace_id, review_id, decision, reason_code, note_label, decided_at, created_at
         FROM review_decisions
         WHERE workspace_id = ?
         ORDER BY decided_at DESC, id ASC`)
            .all(workspaceId);
        const result = [];
        for (const row of rows) {
            if (!isRecord(row))
                continue;
            const record = {
                id: String(row.id),
                workspaceId: String(row.workspace_id),
                reviewId: String(row.review_id),
                decision: row.decision,
                decidedAt: String(row.decided_at),
                createdAt: String(row.created_at),
            };
            if (typeof row.reason_code === "string")
                record.reasonCode = row.reason_code;
            if (typeof row.note_label === "string")
                record.noteLabel = row.note_label;
            result.push(record);
        }
        return result;
    }
}
