/** Human-readable NAICS labels + approximate federal contract volume (2020–2026, USAspending) */
export const NAICS_LABELS: Record<string, { label: string; contractsSince2020: number; smallBusinessRange: number }> = {
  "238220": { label: "Plumbing, HVAC & mechanical contractors", contractsSince2020: 24904, smallBusinessRange: 15155 },
  "238160": { label: "Roofing contractors", contractsSince2020: 8200, smallBusinessRange: 5100 },
  "238110": { label: "Poured concrete & structural contractors", contractsSince2020: 11200, smallBusinessRange: 6800 },
  "238210": { label: "Electrical contractors", contractsSince2020: 16749, smallBusinessRange: 10521 },
  "561730": { label: "Landscaping & grounds maintenance", contractsSince2020: 18680, smallBusinessRange: 11730 },
  "561720": { label: "Janitorial & building cleaning", contractsSince2020: 24146, smallBusinessRange: 12610 },
  "561790": { label: "Other building & dwelling services", contractsSince2020: 4313, smallBusinessRange: 2688 },
  "236118": { label: "Residential remodelers", contractsSince2020: 3200, smallBusinessRange: 2100 },
  "236220": { label: "Commercial building construction", contractsSince2020: 18500, smallBusinessRange: 9200 },
  "541511": { label: "Custom computer programming", contractsSince2020: 42000, smallBusinessRange: 22000 },
  "541512": { label: "Computer systems design", contractsSince2020: 38000, smallBusinessRange: 19000 },
  "541611": { label: "Administrative & general management consulting", contractsSince2020: 22000, smallBusinessRange: 14000 },
  "621111": { label: "Physicians (except mental health)", contractsSince2020: 8500, smallBusinessRange: 4200 },
  "621210": { label: "Dental offices", contractsSince2020: 4100, smallBusinessRange: 2800 },
  "722511": { label: "Full-service restaurants", contractsSince2020: 6200, smallBusinessRange: 3900 },
  "811111": { label: "General automotive repair", contractsSince2020: 5400, smallBusinessRange: 3200 },
  "541990": { label: "All other professional services", contractsSince2020: 15000, smallBusinessRange: 9000 },
};

export function describeNaics(code: string): string {
  return NAICS_LABELS[code]?.label ?? `NAICS ${code}`;
}

export function getMarketStatsForNaics(codes: string[]) {
  let totalContracts = 0;
  let smallBusinessContracts = 0;
  const labels: string[] = [];

  for (const code of codes) {
    const entry = NAICS_LABELS[code];
    if (entry) {
      totalContracts += entry.contractsSince2020;
      smallBusinessContracts += entry.smallBusinessRange;
      labels.push(`${code} — ${entry.label}`);
    } else {
      labels.push(code);
    }
  }

  return { totalContracts, smallBusinessContracts, labels };
}
