// app.jsx — root of the portfolio; wires sections + Tweaks panel.

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "accentHue": 256,
  "accentName": "Indigo",
  "density": "regular",
  "heroVariant": "split"
}/*EDITMODE-END*/;

const ACCENT_OPTIONS = [
  { name: 'Indigo',  hue: 256, swatch: '#4A5DD8' },
  { name: 'Ocre',    hue:  70, swatch: '#A88431' },
  { name: 'Vert',    hue: 145, swatch: '#3A8B59' },
  { name: 'Ardoise', hue: 230, swatch: '#5C6478' },
];

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);

  // Apply density to <html>
  React.useEffect(() => {
    document.documentElement.dataset.density = t.density;
  }, [t.density]);

  // Apply accent hue
  React.useEffect(() => {
    const root = document.documentElement;
    root.style.setProperty('--accent', `oklch(0.50 0.16 ${t.accentHue})`);
    root.style.setProperty('--accent-soft', `oklch(0.50 0.16 ${t.accentHue} / 0.10)`);
    root.style.setProperty('--accent-ink', `oklch(0.30 0.10 ${t.accentHue})`);
  }, [t.accentHue]);

  // Live favicon that follows the accent color.
  // NOTE: the static favicons in <head> are what Google + crawlers see.
  // This effect only adds an extra <link id="dynamic-favicon"> for live tab updates
  // when the user changes accent — it never replaces the static ones.
  React.useEffect(() => {
    const accent = ACCENT_OPTIONS.find(o => o.hue === t.accentHue)?.swatch || '#4A5DD8';
    const bg = '#0E0F0C';
    const fg = '#FAFAF7';
    const svg =
      `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">` +
        `<rect width="32" height="32" rx="7" fill="${bg}"/>` +
        `<path d="M32 0 L20 0 L32 12 Z" fill="${accent}"/>` +
        `<text x="16" y="22" font-family="ui-monospace,SFMono-Regular,Menlo,Consolas,monospace" ` +
              `font-size="15" font-weight="700" fill="${fg}" text-anchor="middle" ` +
              `letter-spacing="-1.2">ih</text>` +
      `</svg>`;
    const href = 'data:image/svg+xml;utf8,' + encodeURIComponent(svg);
    let link = document.getElementById('dynamic-favicon');
    if (!link) {
      link = document.createElement('link');
      link.id = 'dynamic-favicon';
      link.rel = 'icon';
      link.type = 'image/svg+xml';
      // Append last so it takes precedence over the static <link rel="icon"> entries
      // for the live browser tab, but only after JS has loaded.
      document.head.appendChild(link);
    }
    link.href = href;
  }, [t.accentHue]);

  return (
    <React.Fragment>
      <Nav />
      <div className="page">
        <Hero />
        <Pivot />
        <Projets />
        <Parcours />
        <HorsDuClavier />
        <Contact />
      </div>

      <FloatingCTA />

      <TweaksPanel title="Tweaks">
        <TweakSection label="Accent" />
        <TweakColor
          label="Couleur"
          value={ACCENT_OPTIONS.find(o => o.hue === t.accentHue)?.swatch || '#4A5DD8'}
          options={ACCENT_OPTIONS.map(o => o.swatch)}
          onChange={(swatch) => {
            const opt = ACCENT_OPTIONS.find(o => o.swatch === swatch);
            if (opt) setTweak({ accentHue: opt.hue, accentName: opt.name });
          }}
        />

        <TweakSection label="Apparence" />
        <TweakRadio
          label="Densité"
          value={t.density}
          options={['compact','regular','airy']}
          onChange={(v) => setTweak('density', v)}
        />
      </TweaksPanel>
    </React.Fragment>
  );
}

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