import { notFound } from "next/navigation";

import { ZoneForm } from "../_components/zone-form/zone-form";
import { fetchBackendZoneById, fetchBackendZoneCountryOptions } from "../_lib/zones-server-api";

type PageProps = {
  params: Promise<{ id: string }>;
};

export default async function ZoneUpdatePage({ params }: PageProps) {
  const { id } = await params;
  const zoneId = Number(id);

  if (!Number.isFinite(zoneId)) {
    notFound();
  }

  const [{ zone }, countryOptions] = await Promise.all([
    fetchBackendZoneById(zoneId),
    fetchBackendZoneCountryOptions(),
  ]);

  if (!zone) {
    notFound();
  }

  return <ZoneForm mode="update" initial={zone} countryOptions={countryOptions} />;
}
