/* global React, ReactDOM */
/* ============================================================
   MYTHOS APP — same routes as the original site, rendered
   through the Mythos chrome and home. Inner pages are the
   existing components, re-themed via the token system.
   ============================================================ */
const { useEffect, useState } = React;

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "mode": "light",
  "accent": "lumen",
  "display": "cormorant",
  "hero": "block110",
  "density": "spacious",
  "motion": "full"
}/*EDITMODE-END*/;

function MythosApp() {
  const VALID_ROUTES = ['home', 'optimize', 'pricing', 'contact', 'revscout-s', 'revtoolbox', 'emi', 'revfield', 'revsizer', 'case-study', 'faq', 'privacy'];
  const LEGACY_ROUTES = { 'tools': 'revscout-s', 'services': 'revscout-s' };
  const normalizeRoute = (h) => {
    if (LEGACY_ROUTES[h]) return LEGACY_ROUTES[h];
    return VALID_ROUTES.includes(h) ? h : 'home';
  };

  const [route, setRoute] = useState(() => {
    const h = (window.location.hash || '#home').replace('#', '');
    return normalizeRoute(h);
  });
  const [tweaks, setTweak] = window.useTweaks(TWEAK_DEFAULTS);
  window.useReveal(route);
  window.useMythosMotion(route, tweaks.motion);

  // Reflect tweaks → attributes on <html>. data-mythos gates every override.
  useEffect(() => {
    const root = document.documentElement;
    root.setAttribute('data-mythos', '1');
    root.setAttribute('data-myth-mode', tweaks.mode);
    root.setAttribute('data-theme', tweaks.mode === 'dark' ? 'dark' : 'light');
    root.setAttribute('data-density', tweaks.density);
    root.setAttribute('data-myth-accent', tweaks.accent);
    root.setAttribute('data-myth-display', tweaks.display);
    root.setAttribute('data-myth-motion', tweaks.motion);
  }, [tweaks]);

  // Route → hash + scroll top
  useEffect(() => {
    if (window.location.hash !== '#' + route) {
      window.history.replaceState(null, '', '#' + route);
    }
    window.scrollTo({ top: 0, behavior: 'instant' });
  }, [route]);

  useEffect(() => {
    const onHash = () => {
      const h = (window.location.hash || '#home').replace('#', '');
      setRoute(normalizeRoute(h));
    };
    window.addEventListener('hashchange', onHash);
    return () => window.removeEventListener('hashchange', onHash);
  }, []);

  // Global audio kill-switch — keep every video muted.
  useEffect(() => {
    let raf;
    const tick = () => {
      document.querySelectorAll('video').forEach((v) => {
        if (!v.muted) v.muted = true;
        if (v.volume !== 0) v.volume = 0;
      });
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, []);

  const inner = (page) => <div className="page-pad">{page}</div>;

  return (
    <div className="shell">
      <window.ScrollProgress />
      <window.MythosNav route={route} setRoute={setRoute} />
      {route === 'home' && <window.MythosHomePage setRoute={setRoute} heroMedia={tweaks.hero} mode={tweaks.mode} />}
      {route === 'revscout-s' && inner(<window.RevScoutSPage setRoute={setRoute} />)}
      {route === 'revtoolbox' && inner(<window.RevToolboxPage setRoute={setRoute} />)}
      {route === 'emi' && inner(<window.EMIPage setRoute={setRoute} />)}
      {route === 'revfield' && inner(<window.RevFieldPage setRoute={setRoute} />)}
      {route === 'revsizer' && inner(<window.RevSizerPage setRoute={setRoute} />)}
      {route === 'optimize' && inner(<window.OptimizePage setRoute={setRoute} />)}
      {route === 'pricing' && inner(<window.PricingPage setRoute={setRoute} />)}
      {route === 'contact' && inner(<window.ContactPage setRoute={setRoute} />)}
      {route === 'case-study' && inner(<window.CaseStudyPage setRoute={setRoute} />)}
      {route === 'faq' && inner(<window.FAQPage setRoute={setRoute} />)}
      {route === 'privacy' && inner(<window.PrivacyPage setRoute={setRoute} />)}
      <window.MythosFooter setRoute={setRoute} />

      <window.TweaksPanel title="Tweaks">
        <window.TweakSection title="Mythos">
          <window.TweakRadio label="Mode" value={tweaks.mode}
            options={[
              { value: 'light', label: 'Daylight' },
              { value: 'dark', label: 'Night' },
            ]}
            onChange={v => setTweak('mode', v)} />
          <window.TweakRadio label="Accent" value={tweaks.accent}
            options={[
              { value: 'lumen', label: 'Lumen' },
              { value: 'verdant', label: 'Verdant' },
              { value: 'bronze', label: 'Bronze' },
            ]}
            onChange={v => setTweak('accent', v)} />
          <window.TweakRadio label="Display face" value={tweaks.display}
            options={[
              { value: 'cormorant', label: 'Cormorant' },
              { value: 'instrument', label: 'Instrument' },
            ]}
            onChange={v => setTweak('display', v)} />
        </window.TweakSection>
        <window.TweakSection title="Hero">
          <window.TweakRadio label="Backdrop" value={tweaks.hero}
            options={[
              { value: 'block110', label: '3D scroll' },
              { value: 'video', label: 'Video' },
              { value: 'still', label: 'Still' },
            ]}
            onChange={v => setTweak('hero', v)} />
        </window.TweakSection>
        <window.TweakSection title="Layout">
          <window.TweakRadio label="Density" value={tweaks.density}
            options={[{ value: 'spacious', label: 'Spacious' }, { value: 'compact', label: 'Compact' }]}
            onChange={v => setTweak('density', v)} />
        </window.TweakSection>
        <window.TweakSection title="Motion">
          <window.TweakRadio label="Scroll animation" value={tweaks.motion}
            options={[
              { value: 'off', label: 'Off' },
              { value: 'subtle', label: 'Subtle' },
              { value: 'full', label: 'Full' },
            ]}
            onChange={v => setTweak('motion', v)} />
        </window.TweakSection>
      </window.TweaksPanel>
    </div>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<MythosApp />);
