import { CalculatorWizard } from "@/components/calculator/calculator-wizard";
import { getServerAuthSession } from "@/lib/auth";
import { getCalculationById } from "@/services/calculation.service";
import type { InheritanceInput } from "@/types/inheritance";

interface CalculatorPageProps {
  searchParams: Promise<{ reopen?: string }>;
}

export default async function CalculatorPage({ searchParams }: CalculatorPageProps) {
  const params = await searchParams;
  const session = await getServerAuthSession();
  let initialInput: InheritanceInput | undefined;

  if (params.reopen && session?.user?.id) {
    const previous = await getCalculationById(params.reopen, session.user.id);
    if (previous) {
      initialInput = previous.inputJson;
    }
  }

  return (
    <section className="bg-background">
      <div className="mx-auto w-full max-w-7xl px-4 pt-8 sm:px-6 lg:px-8">
        <h1 className="text-2xl font-semibold text-emerald-950 sm:text-3xl">
          Inheritance Calculator
        </h1>
        <p className="mt-2 text-sm text-muted-foreground">
          Hanafi calculation wizard with review and export-ready results.
        </p>
      </div>
      <CalculatorWizard initialInput={initialInput} />
    </section>
  );
}
