import { getPortfolio } from "../../utils";
import { listScenariosWithStatus } from "@/lib/scenarios/queries";
import { scenarioIdsWithActiveRuns } from "@/lib/modellingRuns/server";
import RunModellingClient, { ScenarioOption } from "./RunModellingClient";

export const dynamic = "force-dynamic";

export default async function Page(props: { params: Promise<{ slug: string }> }) {
  const { slug } = await props.params;
  const [{ name }, scenarios, activeIds] = await Promise.all([
    getPortfolio(slug),
    listScenariosWithStatus(BigInt(slug)),
    scenarioIdsWithActiveRuns(slug),
  ]);

  const options: ScenarioOption[] = scenarios.map((s) => ({
    id: s.id.toString(),
    name: s.name ?? `Scenario ${s.id}`,
    goal: s.goal,
    goalValue: s.goalValue,
    housingType: s.housingType,
    budget: s.budget,
    status: activeIds.has(s.id.toString())
      ? "running"
      : s.status === "modelled"
        ? "modelled"
        : "awaiting",
    modelledCount: s.plansCount,
  }));

  return (
    <RunModellingClient
      portfolioId={slug}
      portfolioName={name ?? "Portfolio"}
      scenarios={options}
    />
  );
}
