/** Maps BrandLxft taxonomy categories and industry names to NAICS codes for SAM.gov */
export const INDUSTRY_NAICS: Record<string, string[]> = {
  Healthcare: ["621111", "621210", "621493", "621610", "423450"],
  "Home Services": ["238220", "238160", "561730", "561790", "236118"],
  Construction: ["236220", "238110", "238910", "237310"],
  Automotive: ["811111", "441110", "488410"],
  "Real Estate": ["531210", "531312", "531390"],
  Legal: ["541110", "541199"],
  Financial: ["541211", "524210", "522310"],
  Beauty: ["812111", "812112", "621999"],
  Fitness: ["713940", "611620"],
  "Restaurants & Food": ["722511", "722320", "722310", "311811"],
  Retail: ["445110", "448140", "443142"],
  "E-Commerce": ["454110", "454390"],
  Technology: ["541511", "541512", "518210", "541519"],
  Marketing: ["541810", "541613", "541830"],
  Education: ["611110", "611710", "611430"],
  Manufacturing: ["311999", "337121", "315990", "326199"],
  Logistics: ["484121", "488510", "493110"],
  "Professional Services": ["541611", "541612", "561311"],
  Entertainment: ["711410", "541921", "512110"],
  Nonprofits: ["813219", "813110"],
  Agriculture: ["111998", "111421", "115112"],
  Travel: ["561510", "721110", "721199"],
  // Legacy / resolved industry names
  Plumbing: ["238220"],
  "Plumbing Company": ["238220"],
  Landscaping: ["561730"],
  HVAC: ["238220"],
  "Medical Products": ["423450", "339112", "423460"],
  Dental: ["621210"],
  "SaaS & Technology": ["541511", "541512", "518210"],
  "B2B SaaS": ["541511", "541512"],
  "Food & Hospitality": ["722511", "722320"],
  "Health & Fitness": ["713940"],
  "Beauty & Wellness": ["812111", "621999"],
  Roofing: ["238160"],
  "Roofing Company": ["238160"],
  Other: ["541990"],
};

const STATE_ABBREVIATIONS: Record<string, string> = {
  alabama: "AL",
  alaska: "AK",
  arizona: "AZ",
  arkansas: "AR",
  california: "CA",
  colorado: "CO",
  connecticut: "CT",
  delaware: "DE",
  florida: "FL",
  georgia: "GA",
  hawaii: "HI",
  idaho: "ID",
  illinois: "IL",
  indiana: "IN",
  iowa: "IA",
  kansas: "KS",
  kentucky: "KY",
  louisiana: "LA",
  maine: "ME",
  maryland: "MD",
  massachusetts: "MA",
  michigan: "MI",
  minnesota: "MN",
  mississippi: "MS",
  missouri: "MO",
  montana: "MT",
  nebraska: "NE",
  nevada: "NV",
  "new hampshire": "NH",
  "new jersey": "NJ",
  "new mexico": "NM",
  "new york": "NY",
  "north carolina": "NC",
  "north dakota": "ND",
  ohio: "OH",
  oklahoma: "OK",
  oregon: "OR",
  pennsylvania: "PA",
  "rhode island": "RI",
  "south carolina": "SC",
  "south dakota": "SD",
  tennessee: "TN",
  texas: "TX",
  utah: "UT",
  vermont: "VT",
  virginia: "VA",
  washington: "WA",
  "west virginia": "WV",
  wisconsin: "WI",
  wyoming: "WY",
  "district of columbia": "DC",
};

export function getNaicsForIndustry(industry: string): string[] {
  return INDUSTRY_NAICS[industry] ?? INDUSTRY_NAICS.Other;
}

/** Parse "Nashville, TN" or "Portland, Oregon" into a 2-letter state code */
export function parseStateCode(location: string): string | undefined {
  if (!location?.trim()) return undefined;

  const parts = location.split(",").map((p) => p.trim());
  if (parts.length >= 2) {
    const candidate = parts[parts.length - 1].toUpperCase();
    if (/^[A-Z]{2}$/.test(candidate)) return candidate;
    const fromName = STATE_ABBREVIATIONS[parts[parts.length - 1].toLowerCase()];
    if (fromName) return fromName;
  }

  const remote = location.toLowerCase();
  if (remote.includes("remote") || remote.includes("nationwide")) return undefined;

  for (const [name, code] of Object.entries(STATE_ABBREVIATIONS)) {
    if (remote.includes(name)) return code;
  }

  return undefined;
}
