/**
 * GET /api/eterra/counties
 *
 * Returns distinct county names from GisUat, sorted alphabetically.
 */
import { NextResponse } from "next/server";
import { prisma } from "@/core/storage/prisma";

export const runtime = "nodejs";
export const dynamic = "force-dynamic";

export async function GET() {
  try {
    const rows = await prisma.gisUat.findMany({
      where: { county: { not: null } },
      select: { county: true },
      distinct: ["county"],
      orderBy: { county: "asc" },
    });
    const counties = rows.map((r) => r.county).filter(Boolean) as string[];
    return NextResponse.json({ counties });
  } catch (error) {
    const msg = error instanceof Error ? error.message : "Eroare la interogare judete";
    return NextResponse.json({ error: msg }, { status: 500 });
  }
}
