import { debugRevealEventSelectSql } from "../internal/sql.js";
import { debugRevealSessionStatusFromEvent, isRecord, mapDebugRevealEvent, } from "../internal/rowMappers.js";
export class DebugRevealRepository {
    #db;
    constructor(db) {
        this.#db = db;
    }
    appendDebugRevealEvent(input) {
        const now = new Date().toISOString();
        const eventAt = input.eventAt ?? now;
        const surfacesJson = JSON.stringify(input.surfaces ?? []);
        this.#db
            .prepare(`INSERT INTO debug_reveal_events
           (id, workspace_id, reveal_session_id, kind, event_at, expires_at, ttl_seconds, reason_label, surfaces_json, created_at)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
            .run(input.id, input.workspaceId, input.revealSessionId, input.kind, eventAt, input.expiresAt ?? null, input.ttlSeconds ?? null, input.reasonLabel ?? null, surfacesJson, now);
        return this.getDebugRevealEvent(input.id);
    }
    getDebugRevealStatus(input) {
        const latest = this.findLatestDebugRevealEvent(input.workspaceId);
        const markerCount = this.countDebugRevealEvents(input.workspaceId);
        if (!latest) {
            return {
                workspaceId: input.workspaceId,
                state: "inactive",
                active: false,
                markerCount,
                cloudSync: "disabled",
                rawStorage: "disabled",
            };
        }
        const lastEvent = {
            id: latest.id,
            kind: latest.kind,
            eventAt: latest.eventAt,
        };
        const current = debugRevealSessionStatusFromEvent(latest, input.now ?? new Date());
        if (latest.kind === "revoked") {
            return {
                workspaceId: input.workspaceId,
                state: "revoked",
                active: false,
                markerCount,
                cloudSync: "disabled",
                rawStorage: "disabled",
                lastEvent,
            };
        }
        if (latest.kind === "expired") {
            return {
                workspaceId: input.workspaceId,
                state: "expired",
                active: false,
                markerCount,
                cloudSync: "disabled",
                rawStorage: "disabled",
                lastEvent,
            };
        }
        if (!current || new Date(current.expiresAt) <= (input.now ?? new Date())) {
            return {
                workspaceId: input.workspaceId,
                state: "expired",
                active: false,
                markerCount,
                cloudSync: "disabled",
                rawStorage: "disabled",
                ...(current ? { current } : {}),
                lastEvent,
            };
        }
        return {
            workspaceId: input.workspaceId,
            state: "active",
            active: true,
            markerCount,
            cloudSync: "disabled",
            rawStorage: "disabled",
            current,
            lastEvent,
        };
    }
    listDebugRevealEvents(workspaceId) {
        return this.#db
            .prepare(`${debugRevealEventSelectSql("WHERE workspace_id = ?")} ORDER BY event_at ASC, id ASC`)
            .all(workspaceId)
            .map(mapDebugRevealEvent);
    }
    getDebugRevealEvent(id) {
        const event = this.#db
            .prepare(debugRevealEventSelectSql("WHERE id = ?"))
            .get(id);
        return mapDebugRevealEvent(event);
    }
    findLatestDebugRevealEvent(workspaceId) {
        const event = this.#db
            .prepare(`${debugRevealEventSelectSql("WHERE workspace_id = ?")} ORDER BY event_at DESC, id DESC LIMIT 1`)
            .get(workspaceId);
        return isRecord(event) ? mapDebugRevealEvent(event) : undefined;
    }
    countDebugRevealEvents(workspaceId) {
        const row = this.#db
            .prepare("SELECT COUNT(*) AS count FROM debug_reveal_events WHERE workspace_id = ?")
            .get(workspaceId);
        if (!isRecord(row) || typeof row.count !== "number")
            return 0;
        return row.count;
    }
}
