import { notFound } from "next/navigation";
import { requireWehrAdmin } from "@/lib/auth/guards";
import { getVehicleForBrigade } from "@/server/data/vehicles";
import {
  getMerkmaleForTemplate,
  getMerkmalValuesForEntity,
} from "@/server/data/merkmale";
import { de } from "@/lib/i18n/de";
import { VehicleForm } from "@/components/verwaltung/VehicleForm";
import { VehicleControls } from "./VehicleControls";

export default async function FahrzeugBearbeitenPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const s = await requireWehrAdmin();
  const { id } = await params;

  // Scoping: fremde/nicht existente Fahrzeuge -> 404 (kein Daten-Leak).
  const vehicle = await getVehicleForBrigade(id, s.user.brigadeId);
  if (!vehicle) notFound();

  const defs = vehicle.templateId
    ? await getMerkmaleForTemplate(vehicle.templateId)
    : [];
  const werte = await getMerkmalValuesForEntity("vehicle", vehicle.id);

  return (
    <div className="space-y-6">
      <h1 className="font-display text-2xl font-semibold text-navy">
        {de.verwaltung.fahrzeugBearbeiten}
      </h1>

      <VehicleControls vehicleId={vehicle.id} status={vehicle.status} />

      <VehicleForm
        mode="edit"
        vehicleId={vehicle.id}
        initial={{
          name: vehicle.name,
          funkrufname: vehicle.funkrufname ?? "",
          notiz: vehicle.notiz ?? "",
        }}
        definitionen={defs}
        vorhandeneWerte={werte}
      />
    </div>
  );
}
