/**
 * Loopback aggregate + review/proof projection routes.
 *
 * GET /workspaces/{wsId}/review-queue
 * GET /workspaces/{wsId}/proof-cards
 * GET /workspaces/{wsId}/capabilities/metrics
 * GET /workspaces/{wsId}/capabilities/overview
 * GET /workspaces/{wsId}/capabilities/overview-dashboard
 *
 * Behind bridge:read scope (via the route table) and workspace authorization.
 */
import { MetricsProjectionService } from "../capabilities/metricsProjectionService.js";
import { OverviewDashboardProjectionService } from "../capabilities/overviewDashboardProjectionService.js";
import { OverviewProjectionService } from "../capabilities/overviewProjectionService.js";
import { ProofCardProjectionService } from "../runs/proofCardProjectionService.js";
import { ReviewProjectionService } from "../runs/reviewProjectionService.js";
import { authorizeWorkspace } from "./authGuards.js";
import { PUBLIC_IDENTIFIER_SCAN_OPTIONS, writeJson } from "./publicResponse.js";
import { firstCapture, matchPath } from "./routeMatching.js";
export async function handleAggregateRoutes(context) {
    const { app, auth, request, response, url } = context;
    if (request.method !== "GET")
        return false;
    const reviewMatch = matchPath(url.pathname, /^\/workspaces\/([^/]+)\/review-queue$/);
    if (reviewMatch) {
        const workspaceId = firstCapture(reviewMatch);
        if (!authorizeWorkspace(response, auth, workspaceId))
            return true;
        const service = new ReviewProjectionService(app.localStore);
        writeJson(response, 200, service.listReviewQueue(workspaceId), {
            surface: "workspace.review-queue",
            scanOptions: PUBLIC_IDENTIFIER_SCAN_OPTIONS,
        });
        return true;
    }
    const proofMatch = matchPath(url.pathname, /^\/workspaces\/([^/]+)\/proof-cards$/);
    if (proofMatch) {
        const workspaceId = firstCapture(proofMatch);
        if (!authorizeWorkspace(response, auth, workspaceId))
            return true;
        const service = new ProofCardProjectionService(app.localStore);
        writeJson(response, 200, service.listProofCards(workspaceId), {
            surface: "workspace.proof-cards",
            scanOptions: PUBLIC_IDENTIFIER_SCAN_OPTIONS,
        });
        return true;
    }
    const metricsMatch = matchPath(url.pathname, /^\/workspaces\/([^/]+)\/capabilities\/metrics$/);
    if (metricsMatch) {
        const workspaceId = firstCapture(metricsMatch);
        if (!authorizeWorkspace(response, auth, workspaceId))
            return true;
        const service = new MetricsProjectionService(app.localStore);
        writeJson(response, 200, service.buildMetrics(workspaceId), {
            surface: "workspace.capabilities.metrics",
            scanOptions: PUBLIC_IDENTIFIER_SCAN_OPTIONS,
        });
        return true;
    }
    const overviewMatch = matchPath(url.pathname, /^\/workspaces\/([^/]+)\/capabilities\/overview$/);
    if (overviewMatch) {
        const workspaceId = firstCapture(overviewMatch);
        if (!authorizeWorkspace(response, auth, workspaceId))
            return true;
        const service = new OverviewProjectionService(app.localStore);
        writeJson(response, 200, service.buildOverview(workspaceId), {
            surface: "workspace.capabilities.overview",
            scanOptions: PUBLIC_IDENTIFIER_SCAN_OPTIONS,
        });
        return true;
    }
    const overviewDashboardMatch = matchPath(url.pathname, /^\/workspaces\/([^/]+)\/capabilities\/overview-dashboard$/);
    if (overviewDashboardMatch) {
        const workspaceId = firstCapture(overviewDashboardMatch);
        if (!authorizeWorkspace(response, auth, workspaceId))
            return true;
        const service = new OverviewDashboardProjectionService(app.localStore);
        writeJson(response, 200, service.buildOverviewDashboard(workspaceId), {
            surface: "workspace.capabilities.overview-dashboard",
            scanOptions: PUBLIC_IDENTIFIER_SCAN_OPTIONS,
        });
        return true;
    }
    return false;
}
