/**
 * Read-only service producing a small overview snapshot.
 */
import { MetricsProjectionService } from "./metricsProjectionService.js";
import { OVERVIEW_PROJECTION_PUBLIC_SOURCE, } from "./overviewProjectionContract.js";
function statusForReadiness(ready, total) {
    if (total === 0)
        return "empty";
    if (ready === total)
        return "ready";
    if (ready === 0)
        return "blocked";
    return "needs-attention";
}
export class OverviewProjectionService {
    store;
    constructor(store) {
        this.store = store;
    }
    buildOverview(workspaceId) {
        const metrics = new MetricsProjectionService(this.store).buildMetrics(workspaceId);
        const c = metrics.counts;
        const runsStatus = c.runsTotal === 0
            ? "empty"
            : c.runsFailed > 0
                ? "needs-attention"
                : "ready";
        const reviewStatus = c.reviewQueueOpen + c.reviewQueueDecided === 0
            ? "empty"
            : c.reviewQueueOpen > 0
                ? "needs-attention"
                : "ready";
        const proofStatus = c.proofEvidence === 0
            ? "empty"
            : c.proofEvidence < c.runsTotal
                ? "needs-attention"
                : "ready";
        const repos = this.store.listRepositoryBindings(workspaceId);
        const gates = this.store.listOnboardingGates(workspaceId);
        const settings = this.store.listSettingsReadiness(workspaceId);
        const reasonCode = c.runsTotal +
            repos.length +
            gates.length +
            settings.length +
            c.permissionActivityRecent ===
            0
            ? "no-data"
            : "ok";
        return {
            contractVersion: "v1",
            generatedAt: new Date().toISOString(),
            source: OVERVIEW_PROJECTION_PUBLIC_SOURCE,
            workspaceId,
            reasonCode,
            surfaces: {
                runs: runsStatus,
                review: reviewStatus,
                proof: proofStatus,
                repositories: statusForReadiness(c.repositoriesConnected, repos.length),
                onboarding: statusForReadiness(c.onboardingReady, gates.length),
                settings: statusForReadiness(c.settingsReady, settings.length),
                permissions: c.permissionActivityRecent === 0 ? "empty" : "ready",
            },
        };
    }
}
