import { isRecord } from "../internal/rowMappers.js";
export class OnboardingRepository {
    #db;
    constructor(db) {
        this.#db = db;
    }
    upsertOnboardingGate(input) {
        const now = new Date().toISOString();
        this.#db
            .prepare(`INSERT INTO onboarding_gates
          (id, workspace_id, gate, status, tone, display_label, last_error_code, created_at, updated_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
         ON CONFLICT(workspace_id, gate) 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.gate, input.status, input.tone, input.displayLabel, input.lastErrorCode ?? null, now, now);
    }
    listOnboardingGates(workspaceId) {
        const rows = this.#db
            .prepare(`SELECT id, workspace_id, gate, status, tone, display_label, last_error_code, created_at, updated_at
         FROM onboarding_gates
         WHERE workspace_id = ?
         ORDER BY gate 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),
                gate: row.gate,
                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;
    }
}
