import { NextResponse } from "next/server";
import { EterraClient } from "@/modules/parcel-sync/services/eterra-client";
import { findLayerById } from "@/modules/parcel-sync/services/eterra-layers";
import { fetchUatGeometry } from "@/modules/parcel-sync/services/uat-geometry";

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

type Body = {
  username?: string;
  password?: string;
  siruta?: string | number;
  layerId?: string;
};

export async function POST(req: Request) {
  try {
    const body = (await req.json()) as Body;
    const username = (
      body.username ??
      process.env.ETERRA_USERNAME ??
      ""
    ).trim();
    const password = (
      body.password ??
      process.env.ETERRA_PASSWORD ??
      ""
    ).trim();
    const siruta = String(body.siruta ?? "").trim();

    if (!username || !password)
      return NextResponse.json({ error: "Credențiale lipsă" }, { status: 400 });
    if (!/^\d+$/.test(siruta))
      return NextResponse.json({ error: "SIRUTA invalid" }, { status: 400 });

    const layerId = body.layerId ? String(body.layerId) : undefined;
    const layer = layerId ? findLayerById(layerId) : undefined;
    if (layerId && !layer)
      return NextResponse.json({ error: "Layer necunoscut" }, { status: 400 });

    const client = await EterraClient.create(username, password);

    let geometry;
    if (layer?.spatialFilter) {
      geometry = await fetchUatGeometry(client, siruta);
    }

    const count = layer
      ? geometry
        ? await client.countLayerByGeometry(layer, geometry)
        : await client.countLayer(layer, siruta)
      : await client.countParcels(siruta);

    return NextResponse.json({ count });
  } catch (error) {
    const message = error instanceof Error ? error.message : "Eroare server";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
