import {
  loadBaselineMetrics,
  getReportingScenarios,
} from "@/app/portfolio/[slug]/(portfolio)/reporting/databaseFunctions";
import { CompareClientArea } from "./CompareClientArea";

export default async function ComparePage(props: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await props.params;
  const portfolioId = Number(slug);

  const [baseline, scenarioRows] = await Promise.all([
    loadBaselineMetrics(portfolioId),
    getReportingScenarios(portfolioId),
  ]);

  // Only modelled scenarios can be compared — the metrics route returns
  // zeros for plan-less scenarios (ADR-0003).
  const scenarios = scenarioRows
    .filter((s) => s.status === "modelled")
    .map((s) => ({
      id: s.id,
      name: s.name,
      goal: s.goal,
      goalValue: s.goalValue,
      budget: s.budget,
    }));

  return (
    <div className="max-w-8xl mx-auto px-6 pb-12 pt-5">
      <CompareClientArea
        portfolioId={portfolioId}
        scenarios={scenarios}
        baseline={{
          avgSap: baseline.averages.avg_sap ?? 0,
          totalCarbon: baseline.totals.total_carbon ?? 0,
          totalBills: baseline.totals.total_bills ?? 0,
          belowC: baseline.epcBands
            .filter((b) => ["D", "E", "F", "G"].includes(b.epc))
            .reduce((s, b) => s + b.actual + b.estimated, 0),
        }}
      />
    </div>
  );
}
