/**
 * Measure grouping for the reporting "where the money goes" allocation and
 * the measures shelf (PRD #370). Pure — the category taxonomy and the
 * roll-up, with no data access. Moved out of the retired
 * ScenarioMeasuresModal so it survives that component's deletion.
 */

export type MeasureCategory =
  | "Wall insulation"
  | "Roof insulation"
  | "Floor insulation"
  | "Ventilation & airtightness"
  | "Windows & glazing"
  | "Solar"
  | "Heating"
  | "Heating controls"
  | "Lighting"
  | "Scaffolding & enabling works"
  | "Other";

export const MEASURE_CATEGORY_MAP: Record<string, MeasureCategory> = {
  internal_wall_insulation: "Wall insulation",
  external_wall_insulation: "Wall insulation",
  cavity_wall_insulation: "Wall insulation",
  cavity_wall_extraction: "Wall insulation",
  loft_insulation: "Roof insulation",
  flat_roof_insulation: "Roof insulation",
  room_roof_insulation: "Roof insulation",
  suspended_floor_insulation: "Floor insulation",
  solid_floor_insulation: "Floor insulation",
  exposed_floor_insulation: "Floor insulation",
  mechanical_ventilation: "Ventilation & airtightness",
  trickle_vent: "Ventilation & airtightness",
  door_undercut: "Ventilation & airtightness",
  sealing_fireplace: "Ventilation & airtightness",
  windows_glazing: "Windows & glazing",
  double_glazing: "Windows & glazing",
  secondary_glazing: "Windows & glazing",
  solar_pv: "Solar",
  solar_battery: "Solar",
  air_source_heat_pump: "Heating",
  boiler_upgrade: "Heating",
  high_heat_retention_storage_heaters: "Heating",
  roomstat_programmer_trvs: "Heating controls",
  time_temperature_zone_control: "Heating controls",
  low_energy_lighting_installation: "Lighting",
  scaffolding: "Scaffolding & enabling works",
};

export function measureCategory(measureType: string): MeasureCategory {
  return MEASURE_CATEGORY_MAP[measureType] ?? "Other";
}

export function measureLabel(measureType: string): string {
  return measureType
    .replaceAll("_", " ")
    .toLowerCase()
    .replace(/\b\w/g, (c) => c.toUpperCase());
}

export interface Measure {
  measureType: string;
  homesCount: number;
  totalCost: number;
  averageCost: number;
}

export interface MeasureGroup {
  category: MeasureCategory;
  rows: Measure[];
  homesTotal: number;
  costTotal: number;
}

/**
 * Rolls measures up by category, ordered by spend (biggest first), rows
 * within a category also by spend. Homes are summed as installations —
 * a home appearing under two measures counts in both, matching how the
 * allocation bar reads ("N installations").
 */
export function groupMeasures(measures: Measure[]): MeasureGroup[] {
  const byCategory = new Map<MeasureCategory, Measure[]>();
  for (const m of measures) {
    const cat = measureCategory(m.measureType);
    if (!byCategory.has(cat)) byCategory.set(cat, []);
    byCategory.get(cat)!.push(m);
  }

  return Array.from(byCategory.entries())
    .map(([category, rows]) => ({
      category,
      rows: [...rows].sort((a, b) => b.totalCost - a.totalCost),
      homesTotal: rows.reduce((s, r) => s + r.homesCount, 0),
      costTotal: rows.reduce((s, r) => s + r.totalCost, 0),
    }))
    .sort((a, b) => b.costTotal - a.costTotal);
}
