import { NextResponse } from "next/server";

import { requireApiAccess } from "@/lib/authorization";
import { getDashboardData } from "@/lib/dashboard";
import type { AppLocale } from "@/lib/i18n";

function normalizeLocale(value: string | null): AppLocale {
  return value === "en" ? "en" : "id";
}

export async function GET(request: Request) {
  const auth = requireApiAccess(request);
  if (!auth.ok) return auth.response;

  const { searchParams } = new URL(request.url);
  const locale = normalizeLocale(searchParams.get("locale"));
  const data = await getDashboardData(locale);

  return NextResponse.json({ data });
}
