import { notFound } from "next/navigation";

import { TaxForm } from "../_components/tax-form/tax-form";
import { fetchBackendTaxById } from "../_lib/taxes-server-api";

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

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

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

  const { tax, errorMessage } = await fetchBackendTaxById(taxId);

  if (errorMessage || !tax) {
    notFound();
  }

  return <TaxForm mode="update" initial={tax} />;
}
