import { isRecord } from "../internal/rowMappers.js";
export class SettingsReadinessRepository {
    #db;
    constructor(db) {
        this.#db = db;
    }
    upsertSettingsReadiness(input) {
        const now = new Date().toISOString();
        this.#db
            .prepare(`INSERT INTO settings_readiness
          (id, workspace_id, area, status, tone, display_label, last_error_code, created_at, updated_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
         ON CONFLICT(workspace_id, area) DO UPDATE SET
           status = excluded.status,
           tone = excluded.tone,
           display_label = excluded.display_label,
           last_error_code = excluded.last_error_code,
           updated_at = excluded.updated_at`)
            .run(input.id, input.workspaceId, input.area, input.status, input.tone, input.displayLabel, input.lastErrorCode ?? null, now, now);
    }
    listSettingsReadiness(workspaceId) {
        const rows = this.#db
            .prepare(`SELECT id, workspace_id, area, status, tone, display_label, last_error_code, created_at, updated_at
         FROM settings_readiness
         WHERE workspace_id = ?
         ORDER BY area 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),
                area: row.area,
                status: row.status,
                tone: row.tone,
                displayLabel: String(row.display_label),
                createdAt: String(row.created_at),
                updatedAt: String(row.updated_at),
            };
            if (typeof row.last_error_code === "string")
                record.lastErrorCode = row.last_error_code;
            result.push(record);
        }
        return result;
    }
}
