import { NextResponse } from "next/server";
import { getSessionPayload } from "@/lib/auth/session";
import { listMunicipalities } from "@/lib/licitations/query";

export async function GET(request: Request) {
  const session = await getSessionPayload();

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

  const url = new URL(request.url);
  const state = url.searchParams.get("state");
  const municipalities = await listMunicipalities({ state });

  return NextResponse.json({
    municipalities,
  });
}
