import { notFound } from "next/navigation";

import { PlanCreateForm } from "../_components/plan-form/plan-create-form";
import { fetchBackendMembershipPlanById } from "../_lib/membership-plans-server-api";

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

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

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

  const { plan, errorMessage } = await fetchBackendMembershipPlanById(planId);

  if (!plan) {
    notFound();
  }

  return <PlanCreateForm mode="update" initial={plan} initialErrorMessage={errorMessage} />;
}
