/* taxonomy.jsx — v0.3.1 hospitality category tree
   ============================================================
   ORDER (locked): Hotels → Restaurants → Bars → Fitness → Wellness → Renovations
   All branches are PARENTS with children. Headliners marked.
   - Hotels   : Boutique ★ HEADLINER, Signature Brand, Franchise, Guest House, Hostal, AirBNB
   - Restaurants : Fine Dining, Design Restaurant, Counter · QSR, Food Truck, Seafood ★ HEADLINER, Franchise, Denominación de Origen, Food Market
   - Bars     : Beer Garden ★ HEADLINER, Cocktail, Wine, Speakeasy, Karaoke, Experience, Signature Franchise
   - Fitness  : HYROX ★ HEADLINER, Padel, Tennis, CrossFit, Boxing, Climbing
   - Wellness : Spa, Thermal, Recovery, Meditation, Hammam
   - Renovations : Heritage, Adaptive Reuse, Restoration, Conversion
   Note: Rooftop is a SITE condition (fold 02), not a typology.
*/

const ALL_BRACKETS = ['XS', 'S', 'M', 'L', 'XL'];

/* Bracket capacity ranges by leaf-typology id. */
const BRACKET_RANGES = {
  /* Hotels */
  hotel_boutique:    { XS:'6–14 keys',     S:'14–32 keys',    M:'32–60 keys',     L:'60–96 keys',    XL:'96+ keys' },
  hotel_signature:   { XS:'24–48 keys',    S:'48–96 keys',    M:'96–180 keys',    L:'180–320 keys',  XL:'320+ keys' },
  hotel_franchise:   { XS:'40–80 keys',    S:'80–140 keys',   M:'140–220 keys',   L:'220–340 keys',  XL:'340+ keys' },
  hotel_guesthouse:  { XS:'3–6 keys',      S:'6–10 keys',     M:'10–14 keys',     L:'14–22 keys',    XL:'22+ keys' },
  hotel_hostal:      { XS:'10–24 beds',    S:'24–60 beds',    M:'60–120 beds',    L:'120–200 beds',  XL:'200+ beds' },
  hotel_airbnb:      { XS:'1 unit',        S:'2–4 units',     M:'4–8 units',      L:'8–16 units',    XL:'16+ units' },

  /* Restaurants */
  rest_fine:         { XS:'14–28 seats',   S:'28–56 seats',   M:'56–96 seats',    L:'96–140 seats',  XL:'140+ seats' },
  rest_design:       { XS:'18–40 seats',   S:'40–80 seats',   M:'80–140 seats',   L:'140–220 seats', XL:'220+ seats' },
  rest_counter:      { XS:'8–18 seats',    S:'18–40 seats',   M:'40–72 seats',    L:'72–120 seats',  XL:'120+ seats' },
  rest_foodtruck:    { XS:'1 truck',       S:'1 truck + 8 seats', M:'2 trucks',   L:'fleet · 4',     XL:'fleet · 8+' },
  rest_seafood:      { XS:'18–32 seats',   S:'32–72 seats',   M:'72–120 seats',   L:'120–200 seats', XL:'200+ seats' },
  rest_franchise:    { XS:'24–48 seats',   S:'48–96 seats',   M:'96–160 seats',   L:'160–240 seats', XL:'240+ seats' },
  rest_origen:       { XS:'24–48 seats',   S:'48–96 seats',   M:'96–160 seats',   L:'160–260 seats', XL:'260+ seats' },
  rest_market:       { XS:'6–10 stalls',   S:'10–18 stalls',  M:'18–32 stalls',   L:'32–56 stalls',  XL:'56+ stalls' },

  /* Bars */
  bar_beergarden:    { XS:'40–80 seats',   S:'80–160 seats',  M:'160–280 seats',  L:'280–480 seats', XL:'480+ seats' },
  bar_cocktail:      { XS:'14–28 seats',   S:'28–56 seats',   M:'56–96 seats',    L:'96–140 seats',  XL:'140+ seats' },
  bar_wine:          { XS:'12–24 seats',   S:'24–48 seats',   M:'48–80 seats',    L:'80–120 seats',  XL:'120+ seats' },
  bar_speakeasy:     { XS:'12–22 seats',   S:'22–40 seats',   M:'40–64 seats',    L:'64–96 seats',   XL:'96+ seats' },
  bar_karaoke:       { XS:'2 rooms',       S:'4–6 rooms',     M:'6–10 rooms',     L:'10–18 rooms',   XL:'18+ rooms' },
  bar_experience:    { XS:'20–40 seats',   S:'40–80 seats',   M:'80–140 seats',   L:'140–220 seats', XL:'220+ seats' },
  bar_signature:     { XS:'32–64 seats',   S:'64–120 seats',  M:'120–200 seats',  L:'200–320 seats', XL:'320+ seats' },

  /* Fitness */
  hyrox:             { XS:'12 athletes',   S:'20 athletes',   M:'40 athletes',    L:'60 athletes',   XL:'80+ athletes' },
  padel:             { XS:'1 court',       S:'2–4 courts',    M:'4–8 courts',     L:'8–14 courts',   XL:'14+ courts' },
  tennis:            { XS:'1 court',       S:'2 courts',      M:'3–4 courts',     L:'5–8 courts',    XL:'8+ courts' },
  crossfit:          { XS:'8–14 athletes', S:'14–28 athletes',M:'28–48 athletes', L:'48–72 athletes',XL:'72+' },
  boxing:            { XS:'1 ring',        S:'1 ring + 4 bags',M:'2 rings + bags',L:'2 rings + open',XL:'multi-program' },
  climbing:          { XS:'40m² wall',     S:'120m² wall',    M:'280m² wall',     L:'520m² wall',    XL:'1000m²+ wall' },

  /* Wellness */
  spa:               { XS:'4–8 rooms',     S:'8–14 rooms',    M:'14–22 rooms',    L:'22–34 rooms',   XL:'34+ rooms' },
  thermal:           { XS:'80–180m² wet',  S:'180–360m² wet', M:'360–640m² wet',  L:'640–960m² wet', XL:'960m²+' },
  recovery:          { XS:'4–10 stations', S:'10–22 stations',M:'22–40 stations', L:'40–64 stations',XL:'64+' },
  meditation:        { XS:'1 room',        S:'2 rooms',       M:'3–4 rooms',      L:'5–7 rooms',     XL:'8+ rooms' },
  hammam:            { XS:'40–90m²',       S:'90–180m²',      M:'180–320m²',      L:'320–520m²',     XL:'520m²+' },

  /* Renovations */
  reno_heritage:     { XS:'~6K SF',        S:'~12K SF',       M:'~28K SF',        L:'~64K SF',       XL:'~120K SF+' },
  reno_adaptive:     { XS:'~8K SF',        S:'~16K SF',       M:'~36K SF',        L:'~72K SF',       XL:'~140K SF+' },
  reno_restoration:  { XS:'~4K SF',        S:'~10K SF',       M:'~22K SF',        L:'~48K SF',       XL:'~96K SF+' },
  reno_conversion:   { XS:'~10K SF',       S:'~20K SF',       M:'~44K SF',        L:'~88K SF',       XL:'~160K SF+' },
};

/* Bracket → typical built SF defaults by leaf typology.
   These are honest heuristic typicals — used ONLY when the operator
   hasn't entered a specific area. The dashboard surfaces a clay tag
   "◆ BRACKET-DERIVED · NO AREA ENTERED" so the user knows the source.
   Derived from public benchmark studies + Rootherhub BIA library. */
const BRACKET_AREA_DEFAULTS = {
  /* Hotels — SF per bracket (built footprint, all keys + amenities) */
  hotel_boutique:    { XS:3500,   S:8500,   M:18000,  L:36000,  XL:72000 },
  hotel_signature:   { XS:14000,  S:32000,  M:68000,  L:130000, XL:240000 },
  hotel_franchise:   { XS:22000,  S:42000,  M:78000,  L:140000, XL:240000 },
  hotel_guesthouse:  { XS:1200,   S:2200,   M:3600,   L:6000,   XL:9500 },
  hotel_hostal:      { XS:2400,   S:5400,   M:11000,  L:20000,  XL:32000 },
  hotel_airbnb:      { XS:700,    S:1600,   M:3200,   L:6400,   XL:12000 },

  /* Restaurants — SF per bracket (FOH + BOH combined) */
  rest_fine:         { XS:1400,   S:2600,   M:4500,   L:6800,   XL:9800 },
  rest_design:       { XS:1600,   S:3400,   M:6000,   L:9500,   XL:14000 },
  rest_counter:      { XS:600,    S:1200,   M:2400,   L:4000,   XL:6400 },
  rest_foodtruck:    { XS:120,    S:280,    M:480,    L:900,    XL:1800 },
  rest_seafood:      { XS:1500,   S:3200,   M:5500,   L:9000,   XL:14000 },
  rest_franchise:    { XS:1800,   S:3600,   M:6200,   L:9200,   XL:14000 },
  rest_origen:       { XS:1800,   S:3800,   M:6400,   L:10200,  XL:15000 },
  rest_market:       { XS:2200,   S:4400,   M:8000,   L:14000,  XL:22000 },

  /* Bars — SF per bracket */
  bar_beergarden:    { XS:1400,   S:3200,   M:6000,   L:10000,  XL:16000 },
  bar_cocktail:      { XS:900,    S:1800,   M:3200,   L:4800,   XL:6800 },
  bar_wine:          { XS:800,    S:1600,   M:2800,   L:4200,   XL:5800 },
  bar_speakeasy:     { XS:700,    S:1400,   M:2400,   L:3600,   XL:5000 },
  bar_karaoke:       { XS:1400,   S:2800,   M:4200,   L:6800,   XL:10000 },
  bar_experience:    { XS:1200,   S:2400,   M:4200,   L:6800,   XL:10500 },
  bar_signature:     { XS:2200,   S:4000,   M:6800,   L:10500,  XL:16000 },

  /* Fitness — SF per bracket */
  hyrox:             { XS:4500,   S:7500,   M:12000,  L:18000,  XL:28000 },  /* ~50-60 ft run line + zones */
  padel:             { XS:2800,   S:6500,   M:14000,  L:24000,  XL:36000 },  /* 200m²/court + bar/lounge */
  tennis:            { XS:7000,   S:14000,  M:22000,  L:38000,  XL:60000 },
  crossfit:          { XS:2400,   S:4400,   M:7800,   L:12000,  XL:18000 },
  boxing:            { XS:2200,   S:3400,   M:6000,   L:10000,  XL:16000 },
  climbing:          { XS:3000,   S:6500,   M:12000,  L:20000,  XL:34000 },

  /* Wellness — SF per bracket */
  spa:               { XS:2400,   S:4800,   M:8000,   L:13000,  XL:20000 },
  thermal:           { XS:2200,   S:4800,   M:8500,   L:13500,  XL:20000 },
  recovery:          { XS:1600,   S:3600,   M:6500,   L:10500,  XL:16000 },
  meditation:        { XS:800,    S:1500,   M:2800,   L:4400,   XL:6500 },
  hammam:            { XS:1200,   S:2600,   M:4500,   L:7000,   XL:10000 },

  /* Renovations — SF per bracket (already in SF in BRACKET_RANGES, just confirming numbers) */
  reno_heritage:     { XS:6000,   S:12000,  M:28000,  L:64000,  XL:120000 },
  reno_adaptive:     { XS:8000,   S:16000,  M:36000,  L:72000,  XL:140000 },
  reno_restoration:  { XS:4000,   S:10000,  M:22000,  L:48000,  XL:96000 },
  reno_conversion:   { XS:10000,  S:20000,  M:44000,  L:88000,  XL:160000 },
};

/* Capacity unit per leaf — used by CapacityInput */
const UNIT_BY_LEAF = {
  hotel_boutique:'keys', hotel_signature:'keys', hotel_franchise:'keys',
  hotel_guesthouse:'keys', hotel_hostal:'beds', hotel_airbnb:'units',
  rest_fine:'seats', rest_design:'seats', rest_counter:'seats',
  rest_foodtruck:'trucks', rest_seafood:'seats', rest_franchise:'seats',
  rest_origen:'seats', rest_market:'stalls',
  bar_beergarden:'seats', bar_cocktail:'seats', bar_wine:'seats',
  bar_speakeasy:'seats', bar_karaoke:'rooms', bar_experience:'seats', bar_signature:'seats',
  hyrox:'athletes', padel:'courts', tennis:'courts', crossfit:'athletes', boxing:'rings', climbing:'m² wall',
  spa:'rooms', thermal:'m² wet', recovery:'stations', meditation:'rooms', hammam:'m²',
  reno_heritage:'SF', reno_adaptive:'SF', reno_restoration:'SF', reno_conversion:'SF',
};

/* Branch tree — locked order. */
const TAXONOMY = [
  {
    id: 'hotels', kind: 'parent',
    label: 'Hotels',
    sub: 'Where guests sleep. From two keys to two hundred.',
    children: [
      { id:'hotel_boutique',   label:'Boutique Hotel',           headline:true,  gloss:'Independent, design-led, ≤96 keys. The category Rootherhub leads with on the hotel side.' },
      { id:'hotel_signature',  label:'Signature Brand Hotel',    gloss:'Owned-brand or soft-flagged. Editorial identity, full-service operation.' },
      { id:'hotel_franchise',  label:'Franchise Hotel',          gloss:'Branded flag, franchisee-operated. Standards govern, brand contract anchors.' },
      { id:'hotel_guesthouse', label:'Guest House',              gloss:'Host-led, ≤22 keys. Personal scale, residential cadence.' },
      { id:'hotel_hostal',     label:'Hostal',                   gloss:'Bed-led, shared rooms or private. Backpacker through design-hostal.' },
      { id:'hotel_airbnb',     label:'AirBNB · Short-Stay',      gloss:'Single unit through small portfolio. Self-check-in, OTA-led distribution.' },
    ],
  },
  {
    id: 'restaurants', kind: 'parent',
    label: 'Restaurants',
    sub: 'Front-of-house, kitchen, line. Counter to destination.',
    children: [
      { id:'rest_seafood',     label:'Seafood',                  headline:true,  gloss:'Catch-led, ice-counter or destination room. Rootherhub specialty.' },
      { id:'rest_fine',        label:'Fine Dining',              gloss:'Tasting-menu cadence, chef-led, full reservation system.' },
      { id:'rest_design',      label:'Design Restaurant',        gloss:'Editorial dining where the room is the program. Counter through full-service.' },
      { id:'rest_origen',      label:'Denominación de Origen · Gastro Hub', gloss:'Region-anchored. Producers in the room. Origin-traceable menu.' },
      { id:'rest_market',      label:'Food Market',              gloss:'Multi-stall dining hall with anchor concepts, curated tenants, and local energy.' },
      { id:'rest_counter',     label:'Counter · QSR',            gloss:'High-throughput counter service. Limited menu, fast pickup.' },
      { id:'rest_franchise',   label:'Franchise',                gloss:'Branded chain unit. Standards govern build + operation.' },
      { id:'rest_foodtruck',   label:'Food Truck',               gloss:'Mobile or hybrid (truck + small fixed footprint).' },
    ],
  },
  {
    id: 'bars', kind: 'parent',
    label: 'Bars',
    sub: 'Counter-led rooms. Cocktail, wine, late-night.',
    children: [
      { id:'bar_beergarden',   label:'Beer Garden',              headline:true,  gloss:'Outdoor or indoor-outdoor. Long-table cadence, brewery anchor.' },
      { id:'bar_cocktail',     label:'Cocktail Bar',             gloss:'Mixologist-led, intimate room with a crafted menu and precise service rhythm.' },
      { id:'bar_wine',         label:'Wine Bar · Enoteca',       gloss:'Glass-program led, curated cellar. Often paired with light food.' },
      { id:'bar_speakeasy',    label:'Speakeasy',                gloss:'Hidden entry, password cadence, members-feel.' },
      { id:'bar_karaoke',      label:'Karaoke',                  gloss:'Private-room social venue with sound isolation and programmed service.' },
      { id:'bar_experience',   label:'Experience Bar',           gloss:'Programmed, sensory or themed, with an immersive cadence.' },
      { id:'bar_signature',    label:'Signature Franchise Bar',  gloss:'Branded chain unit. Standards govern build + operation.' },
    ],
  },
  {
    id: 'fitness', kind: 'parent',
    label: 'Fitness',
    sub: 'Programmed movement. Where the body is tested.',
    children: [
      { id:'hyrox',     label:'HYROX Training Club',  headline:true, gloss:'Curated stations, race logic, and high-throughput athlete flow.' },
      { id:'padel',     label:'Padel Courts',         gloss:'Lifestyle padel venue: courts, bar, lounge, retail, and post-match social energy.' },
      { id:'tennis',    label:'Tennis Courts',        gloss:'Court-led social club with coaching, events, and viewing zones.' },
      { id:'crossfit',  label:'CrossFit Boxes',       gloss:'Heavy-duty training box with impact flooring, steel rigs, and raw materials.' },
      { id:'boxing',    label:'Boxing',               gloss:'Boxing club with ring, bag line, athletic grit, and heritage-driven atmosphere.' },
      { id:'climbing',  label:'Climbing',             gloss:'Indoor climbing venue where wall geometry, material grip, and social zones meet.' },
    ],
  },
  {
    id: 'wellness', kind: 'parent',
    label: 'Wellness',
    sub: 'Restorative programs. Where the body slows.',
    children: [
      { id:'spa',         label:'Spas',                gloss:'Treatment-led: wet and dry rooms, ritual programming, hands-on practitioners.' },
      { id:'thermal',     label:'Thermal Baths',       gloss:'Multi-temperature water bodies, sequence-based, communal or private.' },
      { id:'recovery',    label:'Recovery Centers',    gloss:'Cryotherapy, IV, percussion and sleep. Performance-side wellness.' },
      { id:'meditation',  label:'Meditation Studios',  gloss:'Single-room programs. Sound, breath, silence.' },
      { id:'hammam',      label:'Hammam',              gloss:'Steam-led ritual baths. Heritage typology, modern revivals.' },
    ],
  },
  {
    id: 'renovations', kind: 'parent',
    label: 'Renovations',
    sub: 'Heritage envelopes. The building tells the brain what it wants to be.',
    children: [
      { id:'reno_heritage',    label:'Heritage',          gloss:'Listed or culturally significant. Codes govern, restraint is the program.' },
      { id:'reno_adaptive',    label:'Adaptive Reuse',    gloss:'New typology in an old envelope: warehouse to market, factory to hotel.' },
      { id:'reno_restoration', label:'Restoration',       gloss:'Period-faithful return. Material authenticity, original cadence.' },
      { id:'reno_conversion',  label:'Conversion',        gloss:'Lighter touch: same use family, modernized envelope and systems.' },
    ],
  },
];

/* Flatten — every leaf is usable across the configurator. */
const ALL_LEAVES = TAXONOMY.flatMap(b => b.children.map(c => ({ ...c, parent: b.id, parentLabel: b.label })));

const leafById = (id) => ALL_LEAVES.find(l => l.id === id) || null;

const combinationStatus = (primaryId, secondaryId) => {
  if (!primaryId) return { kind: 'incomplete' };
  if (!secondaryId) return { kind: 'single' };
  if (primaryId === secondaryId) return { kind: 'invalid', reason: 'same typology twice' };
  return { kind: 'pair' };
};

Object.assign(window, {
  ALL_BRACKETS, BRACKET_RANGES, BRACKET_AREA_DEFAULTS, UNIT_BY_LEAF, TAXONOMY, ALL_LEAVES,
  leafById, combinationStatus,
});
