import { getCuratedDomains, type CuratedDomainStatus } from "../../../db/curatedDomains";

/**
 * GET /api/admin/curated-domains?status=suggested&country=DE
 *
 * Admin-Inbox für User-vorgeschlagene Country-Curated-Domains.
 * Query-Params:
 *   status  — "suggested" | "approved" | "rejected" (optional, default: alle)
 *   country — Ländercode filtern (optional)
 */
export default defineEventHandler(async (event) => {
  const config = useRuntimeConfig();
  const adminSecret = getHeader(event, "x-admin-secret");
  if (adminSecret !== config.adminSecret) {
    throw createError({ statusCode: 401, message: "Unauthorized" });
  }

  const query = getQuery(event);
  const status = query.status as CuratedDomainStatus | undefined;
  const country = query.country as string | undefined;

  const validStatuses: CuratedDomainStatus[] = ["suggested", "approved", "rejected"];
  if (status && !validStatuses.includes(status)) {
    throw createError({
      statusCode: 400,
      data: { error: "INVALID_STATUS", valid: validStatuses },
    });
  }

  const rows = await getCuratedDomains({
    ...(status ? { status } : {}),
    ...(country ? { country: country.toUpperCase() } : {}),
  });

  return { items: rows, total: rows.length };
});
