import { fetchHeaderFooterHomeData } from "../../../lib/serverFetch";
import Footer from "../../../components/layout/Footer";
import SmoothScrollToForm from "../../../components/ui/SmoothScrollToForm";
import { fetchGroupDetailAction } from "../../../lib/actions/groupDetailAction";
import { ClassDetailsProvider } from "@/src/lib/context/ClassDetailsContext";
import { notFound } from "next/navigation";
import RegisterProgramForm from "../../../components/layout/form/RegisterProgramForm";
import { fetchStripeConnectContextAction } from "../../../lib/actions/stripeConnectAction";

export default async function ClassRegistrationDetails({
  params,
  searchParams,
}: {
  params: Promise<{ id?: string }>;
  searchParams: Promise<Record<string, string | string[]>>;
}) {
  const resolvedParams = await params;
  const sParams = await searchParams;
  const headerFooterHomeData = await fetchHeaderFooterHomeData(sParams);

  const classId = resolvedParams?.id;
  const franchiseeId = sParams.franchiseeId as string;

  if (!classId) {
    return <div className="p-8 text-center text-red-600">Class ID not found in URL</div>;
  }

  const [groupDetailResponse, stripeAccountId] = await Promise.all([
    fetchGroupDetailAction(classId, franchiseeId),
    fetchStripeConnectContextAction(),
  ]);

  const classDetails = groupDetailResponse.success ? groupDetailResponse.data : null;

  if (!classDetails) {
    notFound();
  }

  return (
    <ClassDetailsProvider classDetails={classDetails}>
      {/* page-fill: when the form is short (e.g. the "payment not configured"
          notice) the page stopped well above the fold and left blank space under
          the footer. The class opts this page into the sticky-footer rule in
          globals.css, which stretches this first section so the footer lands at
          the bottom of the viewport. */}
      <main className="overflow-x-hidden page-fill">
        <section className="relative w-full overflow-visible z-[999] pt-[40px] page-fill-grow">
          <RegisterProgramForm
            zohoFranchiseId={headerFooterHomeData?.tenant?.zoho_franchise_id}
            stripeAccountId={stripeAccountId}
          />
        </section>

        <section className="relative w-full overflow-hidden mt-[-45px] xl:mt-0 pt-[40px] xl:pt-[80px] max-[767px]:pt-[0px]">
          <Footer footerData={headerFooterHomeData} />
        </section>

        <SmoothScrollToForm />
      </main>
    </ClassDetailsProvider>
  );
}
