import { NextResponse } from "next/server";
import { requireAdminApiUser } from "@/lib/auth/admin";
import { getInstitutionalDossierForUser } from "@/lib/audits/server";

function parseStrategy(value: string | null) {
  return value === "snapshot" ? "snapshot" : "live";
}

export async function GET(request: Request) {
  const user = await requireAdminApiUser();

  if (!user) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const { searchParams } = new URL(request.url);
  const strategy = parseStrategy(searchParams.get("strategy"));
  const resolved = await getInstitutionalDossierForUser(user.id, strategy);

  return NextResponse.json({
    ok: true,
    dossier: resolved.dossier,
    freshness: resolved.freshness,
  });
}
