/* Brainstorm X — App shell: shared state, data, screen routing.
   Screens are exposed on window by their own files (BXWizard, BXInvite,
   BXArena, BXShortlist, BXReport). */
(function () {
  const { useState } = React;
  const DS = window.BrainstormXDesignSystem_f2a458;
  const { Badge, AvatarStack } = DS;
  const I = window.BXIcons;

  // ---- Heat from score (brief §21) -------------------------------------
  window.BXheatFromScore = function (score) {
    if (score >= 12) return 'on_fire';
    if (score >= 6) return 'hot';
    if (score >= 1) return 'warm';
    if (score <= -8) return 'frozen';
    if (score <= -4) return 'cold';
    return 'neutral';
  };

  // ---- Seed name ideas (Pickle Paws — playful pet accessories) ---------
  window.BXseedNames = [
    { id: 'pawlio', name: 'Pawlio', style: 'brandable', pronunciation: 'paw-lee-oh', reason: 'Short, friendly and suitable for a playful pet brand.', keywords: ['pet', 'playful', 'friendly'], confidence: 82, score: 14, counts: { up: 14, down: 1, fire: 6, ice: 0 }, x: 30, y: 18 },
    { id: 'snugglo', name: 'Snugglo', style: 'compound', pronunciation: 'snug-loh', reason: 'Cosy and cute — blends "snuggle" with a soft ending.', keywords: ['cute', 'cosy'], confidence: 74, score: 9, counts: { up: 9, down: 2, fire: 2, ice: 0 }, x: 64, y: 30 },
    { id: 'nuvio', name: 'Nuvio', style: 'evocative', pronunciation: 'noo-vee-oh', reason: 'Modern and energetic, easy to say across markets.', keywords: ['fresh', 'fast'], confidence: 68, score: 5, counts: { up: 6, down: 2, fire: 0, ice: 0 }, x: 12, y: 56 },
    { id: 'picklepip', name: 'Picklepip', style: 'compound', pronunciation: 'pik-uhl-pip', reason: 'Quirky and characterful — leans into the playful brief.', keywords: ['quirky', 'pickle'], confidence: 79, score: 8, counts: { up: 8, down: 1, fire: 1, ice: 0 }, x: 78, y: 60 },
    { id: 'fetchly', name: 'Fetchly', style: 'alternate_spelling', pronunciation: 'fetch-lee', reason: 'Action-led and brandable, clear pet association.', keywords: ['fetch', 'fun'], confidence: 71, score: 3, counts: { up: 4, down: 2, fire: 0, ice: 0 }, x: 46, y: 50 },
    { id: 'whiskr', name: 'Whiskr', style: 'alternate_spelling', pronunciation: 'wis-ker', reason: 'Trendy dropped-vowel spelling, social-handle friendly.', keywords: ['cute', 'cats'], confidence: 64, score: 1, counts: { up: 3, down: 3, fire: 0, ice: 1 }, x: 22, y: 78 },
    { id: 'petpropolis', name: 'Petropolis', style: 'compound', pronunciation: 'pet-rop-oh-lis', reason: 'Bigger, "city of pets" feel — more descriptive.', keywords: ['pet', 'big'], confidence: 58, score: -2, counts: { up: 2, down: 4, fire: 0, ice: 1 }, x: 60, y: 82 },
    { id: 'drabwell', name: 'Drabwell', style: 'real_word', pronunciation: 'drab-wel', reason: 'Felt too flat and corporate for the playful tone.', keywords: ['plain'], confidence: 40, score: -7, counts: { up: 1, down: 9, fire: 0, ice: 4 }, x: 86, y: 16 },
  ];

  window.BXseedKeywords = [
    { id: 'quirky', label: 'quirky', state: 'favourite', weight: 3, count: 5 },
    { id: 'playful', label: 'playful', state: 'boosted', weight: 2, count: 3 },
    { id: 'cute', label: 'cute', state: 'boosted', weight: 2, count: 2 },
    { id: 'paws', label: 'paws', state: 'normal', weight: 1, count: null },
    { id: 'fast', label: 'fast', state: 'cooling', weight: 1, count: null },
    { id: 'corporate', label: 'corporate', state: 'avoid', weight: 1, count: null },
  ];

  window.BXpeople = [
    { name: 'Sam Lee', ring: true }, { name: 'Alex Roy', ring: true },
    { name: 'Priya N', ring: true }, { name: 'Jo Kim' },
  ];

  const SCREENS = [
    { id: 'wizard', label: 'Wizard', ic: 'WandSparkles' },
    { id: 'invite', label: 'Invite', ic: 'Users' },
    { id: 'arena', label: 'Name Arena', ic: 'Compass' },
    { id: 'shortlist', label: 'Shortlist', ic: 'Star' },
    { id: 'report', label: 'Report', ic: 'Layers' },
  ];

  function ScreenTabs({ screen, setScreen }) {
    return (
      <div style={{
        position: 'fixed', bottom: 16, left: '50%', transform: 'translateX(-50%)', zIndex: 60,
        display: 'flex', gap: 4, padding: 6, borderRadius: 'var(--radius-pill)',
        background: 'var(--surface-glass-strong)', border: '1px solid var(--line-2)',
        backdropFilter: 'blur(18px)', WebkitBackdropFilter: 'blur(18px)', boxShadow: 'var(--shadow-lg)',
      }}>
        {SCREENS.map((s) => {
          const Ic = I[s.ic];
          const on = screen === s.id;
          return (
            <button key={s.id} onClick={() => setScreen(s.id)} style={{
              display: 'inline-flex', alignItems: 'center', gap: 7, padding: '8px 14px',
              borderRadius: 'var(--radius-pill)', border: 'none', cursor: 'pointer',
              background: on ? 'var(--neon-cyan)' : 'transparent', color: on ? 'var(--accent-ink)' : 'var(--text-2)',
              fontFamily: 'var(--font-sans)', fontSize: 13, fontWeight: 700,
              boxShadow: on ? 'var(--glow-cyan)' : 'none', transition: 'all var(--dur-base) var(--ease-out)',
            }}><Ic size={15} />{s.label}</button>
          );
        })}
      </div>
    );
  }

  function App() {
    const [screen, setScreen] = useState('wizard');
    const [names, setNames] = useState(window.BXseedNames);
    const [keywords, setKeywords] = useState(window.BXseedKeywords);
    const [wave, setWave] = useState(1);

    const shared = { names, setNames, keywords, setKeywords, wave, setWave, goto: setScreen };

    const screens = {
      wizard: window.BXWizard,
      invite: window.BXInvite,
      arena: window.BXArena,
      shortlist: window.BXShortlist,
      report: window.BXReport,
    };
    const Active = screens[screen] || (() => null);

    return (
      <div style={{ minHeight: '100vh' }}>
        <Active {...shared} />
        <ScreenTabs screen={screen} setScreen={setScreen} />
      </div>
    );
  }

  window.BXApp = App;
})();
