import { listScenariosWithStatus } from "@/lib/scenarios/queries";
import { NewScenarioJourney } from "./NewScenarioJourney";

export const dynamic = "force-dynamic";

export default async function NewScenarioPage(props: {
  params: Promise<{ slug: string }>;
  searchParams: Promise<{ from?: string }>;
}) {
  const [{ slug }, { from }] = await Promise.all([
    props.params,
    props.searchParams,
  ]);
  const scenarios = await listScenariosWithStatus(BigInt(slug));

  // Plain-JSON view of existing scenarios: duplicate detection + housing default.
  const existing = scenarios.map((s) => ({
    id: String(s.id),
    name: s.name ?? `Scenario ${s.id}`,
    housingType: s.housingType,
    goal: s.goal,
    goalValue: s.goalValue,
    budget: s.budget,
    exclusions: s.exclusions,
    fabricFirst: s.fabricFirst,
    modelled: s.status === "modelled",
  }));
  const template = from ? existing.find((s) => s.id === from) ?? null : null;

  return (
    <NewScenarioJourney
      portfolioId={slug}
      existing={existing}
      defaultHousing={existing[0]?.housingType ?? "Social"}
      template={template}
    />
  );
}
