import { notFound } from "next/navigation";

import { CountryForm } from "../_components/country-form/country-form";
import { fetchBackendCountryById } from "../_lib/countries-server-api";
import { fetchBackendCurrencyOptions } from "../../currencies/_lib/currencies-server-api";

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

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

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

  const [{ country }, currencyOptions] = await Promise.all([
    fetchBackendCountryById(countryId),
    fetchBackendCurrencyOptions(),
  ]);

  if (!country) {
    notFound();
  }

  return <CountryForm mode="update" initial={country} currencyOptions={currencyOptions} />;
}
