import type { ReactNode } from "react";
import { PROJECT_DELIVERY_RATE } from "@/lib/reporting/model";
import { TipBody, type TipSpec } from "./tipContent";

const deliveryPct = Math.round(PROJECT_DELIVERY_RATE * 100);

/**
 * Structured "what this figure covers" copy for the scenario cost surfaces —
 * a one-line definition plus scannable Includes/Excludes lists, so the KPI
 * band, the Investment ledger and the "where the money goes" breakdown all
 * explain a term the same way. Wording follows the domain glossary
 * (CONTEXT.md → Reporting): Gross = construction works + project delivery +
 * contingency; Net = gross − secured funding.
 */
const GROSS_PARTS = ["Construction works", "Project delivery", "Contingency"];

const SPECS = {
  grossPerHome: {
    summary: "Gross cost ÷ the homes upgraded in this scenario.",
    includes: GROSS_PARTS,
    excludes: ["Funding (deducted to reach net)", "VAT", "Internal staff time"],
  },
  net: {
    summary:
      "Gross cost minus secured funding — the figure a board commits to.",
    includes: GROSS_PARTS,
    excludes: ["VAT", "Internal staff time"],
    note: "Net = gross − secured funding.",
  },
  construction: {
    summary:
      "The principal contractor's price for the physical works — the measures installed.",
    excludes: ["Project delivery", "Contingency"],
  },
  projectDelivery: {
    summary:
      "Professional and management costs of delivering the works — design, project management, overheads.",
    note: `Estimated at ${deliveryPct}% of construction works.`,
  },
  contingency: {
    summary:
      "A risk allowance for unforeseen costs, on top of construction works and project delivery.",
    note: "Part of gross cost, and so of net.",
  },
  billSavings: {
    summary:
      "Modelled reduction in residents' annual energy bills once the works are in.",
    note: "This scenario vs current stock.",
  },
  carbonSaved: {
    summary: "Modelled annual carbon saved once the works are in.",
    note: "This scenario vs current stock.",
  },
  costPerSap: {
    summary: "Capital cost ÷ total SAP points gained. Lower is better.",
    includes: ["Construction works", "Project delivery"],
    excludes: ["Contingency", "Funding"],
  },
  costPerCarbon: {
    summary: "Capital cost ÷ tonnes of CO₂ saved a year. Lower is better.",
    includes: ["Construction works", "Project delivery"],
    excludes: ["Contingency", "Funding"],
  },
  whereMoneyGoes: {
    summary:
      "The construction works only — the cost of the measures installed.",
    excludes: ["Project delivery", "Contingency"],
    note: 'Totals the "Construction works" ledger line, not gross cost.',
  },
} satisfies Record<string, TipSpec>;

/** Rendered tooltip bodies, keyed by figure. `tip={COST_HELP.net}` etc. */
export const COST_HELP = Object.fromEntries(
  Object.entries(SPECS).map(([key, spec]) => [
    key,
    <TipBody key={key} spec={spec} />,
  ]),
) as Record<keyof typeof SPECS, ReactNode>;
