import { NextRequest, NextResponse } from "next/server";
import { getServerSession } from "next-auth";

import { AuthOptions } from "@/app/api/auth/[...nextauth]/authOptions";
import { getFlaggedAddresses } from "@/lib/bulkUpload/addressMatches";

// Read-only: the combiner rows address2uprn could not confidently match
// (unmatched / ambiguous_duplicate / invalid_postcode), joined with any saved
// correction. Drives the pre-finalise "Addresses" review tab (ADR-0057); the
// Finalise gate blocks until every flagged row is resolved.
export async function GET(
  _request: NextRequest,
  { params }: { params: Promise<{ portfolioId: string; uploadId: string }> },
) {
  const session = await getServerSession(AuthOptions);
  if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const { uploadId } = await params;
  const flagged = await getFlaggedAddresses(uploadId);
  const unresolved = flagged.filter((f) => !f.resolved).length;
  return NextResponse.json({ flagged, unresolved }, { status: 200 });
}
