// app.jsx — composition + Tweaks
// Mounts everything, applies palette via [data-palette] on <html>.

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "palette": ["#9A5566", "#F4EEE6", "#2B1B22"],
  "headline": "editorial",
  "showChannels": true,
  "showCare": true,
  "showLocations": true,
  "showPersonas": true,
  "ariaPosition": "after-personas",
  "displayFont": "instrument",
  "bodyFont": "geist"
}/*EDITMODE-END*/;

const PALETTE_OPTIONS = [
  { id: "rosewood", swatches: ["#9A5566", "#F4EEE6", "#2B1B22"] },
  { id: "snow",     swatches: ["#9A5566", "#FAFAF7", "#14181A"] },
  { id: "forest",   swatches: ["#2C5042", "#F2EEE5", "#131C18"] },
  { id: "clinical", swatches: ["#1F3A6B", "#F6F7F9", "#0E1726"] },
];

function paletteIdFor(swatches) {
  const key = JSON.stringify(swatches);
  const m = PALETTE_OPTIONS.find((p) => JSON.stringify(p.swatches) === key);
  return m ? m.id : "rosewood";
}

const DISPLAY_FONT_MAP = {
  instrument: '"Instrument Serif", Georgia, serif',
  newsreader: '"Newsreader", Georgia, serif',
  dmserif:    '"DM Serif Display", Georgia, serif',
};

const BODY_FONT_MAP = {
  geist:     '"Geist", ui-sans-serif, system-ui, sans-serif',
  manrope:   '"Manrope", ui-sans-serif, system-ui, sans-serif',
  ibmplex:   '"IBM Plex Sans", ui-sans-serif, system-ui, sans-serif',
};

const TONE_HEADLINES = {
  editorial: {
    h1: <React.Fragment>Dental care<br />that meets you<br /><em>where you are.</em></React.Fragment>,
    sub: "Book by WhatsApp, SMS, Telegram, webchat or email — in English or French — and talk to a real clinician within hours. Four clinics across Manitoba, one patient record, zero repeating yourself.",
  },
  clinical: {
    h1: <React.Fragment>Modern dentistry,<br /><em>quietly excellent.</em></React.Fragment>,
    sub: "Four clinics. Twenty-five chairs. One booking system, one chart, one privacy policy. Reach us on any of five channels and we'll route you to the right clinician within hours.",
  },
  warm: {
    h1: <React.Fragment>Smile like you <em>mean it.</em><br />We'll handle the rest.</React.Fragment>,
    sub: "Maplesmile is a family of four Manitoba dental clinics. Message us, call us, walk in — in English or French. We'll remember you next time.",
  },
};

function App() {
  const [t, setTweak] = useTweaks(TWEAK_DEFAULTS);
  const [whyOpen, setWhyOpen] = React.useState(false);
  const openWhy = React.useCallback(() => setWhyOpen(true), []);
  const closeWhy = React.useCallback(() => setWhyOpen(false), []);

  // Apply palette + fonts to root
  React.useEffect(() => {
    document.documentElement.setAttribute("data-palette", paletteIdFor(t.palette));
    document.documentElement.style.setProperty("--font-display", DISPLAY_FONT_MAP[t.displayFont] || DISPLAY_FONT_MAP.instrument);
    document.documentElement.style.setProperty("--font-body",    BODY_FONT_MAP[t.bodyFont]       || BODY_FONT_MAP.geist);
  }, [t.palette, t.displayFont, t.bodyFont]);

  // Inject extra Google Font requests if a non-default font is chosen.
  React.useEffect(() => {
    const families = [];
    if (t.displayFont === "newsreader") families.push("Newsreader:opsz,wght@6..72,300..600");
    if (t.displayFont === "dmserif")    families.push("DM+Serif+Display");
    if (t.bodyFont === "manrope")       families.push("Manrope:wght@300..700");
    if (t.bodyFont === "ibmplex")       families.push("IBM+Plex+Sans:wght@300;400;500;600");
    if (!families.length) return;
    const id = "twk-font-extras";
    let link = document.getElementById(id);
    if (!link) {
      link = document.createElement("link");
      link.id = id;
      link.rel = "stylesheet";
      document.head.appendChild(link);
    }
    link.href = `https://fonts.googleapis.com/css2?${families.map((f) => `family=${f}`).join("&")}&display=swap`;
  }, [t.displayFont, t.bodyFont]);

  const onBook = () => {
    const el = document.getElementById("contact");
    if (el) window.scrollTo({ top: el.offsetTop - 80, behavior: "smooth" });
  };

  const tone = TONE_HEADLINES[t.headline] || TONE_HEADLINES.warm;

  // Decide section order based on ariaPosition
  const sections = {
    channels: t.showChannels && <ChannelsStrip />,
    care:     t.showCare     && <Care />,
    locations: t.showLocations && <Locations />,
    personas: t.showPersonas && <Personas onPersonaWhy={openWhy} />,
    aria:     <AriaWhySection trajectory={TRAJECTORY} onOpen={openWhy} />,
  };

  let order;
  switch (t.ariaPosition) {
    case "after-hero":    order = ["aria", "channels", "care", "locations", "personas"]; break;
    case "after-channels": order = ["channels", "aria", "care", "locations", "personas"]; break;
    case "after-care":    order = ["channels", "care", "aria", "locations", "personas"]; break;
    case "after-locations": order = ["channels", "care", "locations", "aria", "personas"]; break;
    case "after-personas":
    default:              order = ["channels", "care", "locations", "personas", "aria"]; break;
  }

  return (
    <div>
      <Nav onBook={onBook} />
      <HeroTweakable tone={tone} onBook={onBook} onWhy={openWhy} />
      {order.map((k) => <React.Fragment key={k}>{sections[k]}</React.Fragment>)}
      <Trust />
      <Contact />
      <Footer />

      <AriaWhyModal open={whyOpen} onClose={closeWhy} trajectory={TRAJECTORY} />

      <TweaksPanel title="Tweaks">
        <TweakSection label="Palette" />
        <TweakColor
          label="Theme"
          value={t.palette}
          options={PALETTE_OPTIONS.map((p) => p.swatches)}
          onChange={(swatches) => setTweak("palette", swatches)}
        />

        <TweakSection label="Typography" />
        <TweakSelect
          label="Display"
          value={t.displayFont}
          options={[
            { value: "instrument", label: "Instrument Serif" },
            { value: "newsreader", label: "Newsreader" },
            { value: "dmserif",    label: "DM Serif Display" },
          ]}
          onChange={(v) => setTweak("displayFont", v)}
        />
        <TweakSelect
          label="Body"
          value={t.bodyFont}
          options={[
            { value: "geist",   label: "Geist" },
            { value: "manrope", label: "Manrope" },
            { value: "ibmplex", label: "IBM Plex Sans" },
          ]}
          onChange={(v) => setTweak("bodyFont", v)}
        />

        <TweakSection label="Voice" />
        <TweakRadio
          label="Hero headline"
          value={t.headline}
          options={[
            { value: "editorial", label: "Editorial" },
            { value: "clinical",  label: "Clinical" },
            { value: "warm",      label: "Warm" },
          ]}
          onChange={(v) => setTweak("headline", v)}
        />

        <TweakSection label="Layout" />
        <TweakSelect
          label="ARIA Why section sits…"
          value={t.ariaPosition}
          options={[
            { value: "after-hero",      label: "1 · Right after the hero" },
            { value: "after-channels",  label: "2 · After channels" },
            { value: "after-care",      label: "3 · After care" },
            { value: "after-locations", label: "4 · After locations" },
            { value: "after-personas",  label: "5 · After personas (default)" },
          ]}
          onChange={(v) => setTweak("ariaPosition", v)}
        />
        <TweakToggle label="Show channels strip"  value={t.showChannels}  onChange={(v) => setTweak("showChannels", v)} />
        <TweakToggle label="Show care section"    value={t.showCare}      onChange={(v) => setTweak("showCare", v)} />
        <TweakToggle label="Show locations"       value={t.showLocations} onChange={(v) => setTweak("showLocations", v)} />
        <TweakToggle label="Show patient stories" value={t.showPersonas}  onChange={(v) => setTweak("showPersonas", v)} />
      </TweaksPanel>
    </div>
  );
}

// Hero w/ tweakable headline copy
function HeroTweakable({ tone, onBook, onWhy }) {
  return (
    <section id="top" data-screen-label="01 Hero" style={{ position: "relative", overflow: "hidden", padding: "72px 0 96px" }}>
      <div className="container-wide">
        <div className="hero-grid" style={{ display: "grid", gridTemplateColumns: "1.1fr 1fr", gap: 56, alignItems: "start" }}>
          <div style={{ position: "relative", zIndex: 1 }}>
            <div className="section-mark"><span className="eyebrow">Four clinics · Manitoba · est. 2019</span></div>
            <h1 className="display" style={{ fontSize: "clamp(48px, 5.5vw, 92px)", margin: 0 }}>{tone.h1}</h1>
            <p style={{ fontSize: 19, lineHeight: 1.55, maxWidth: 520, color: "var(--ink-soft)", marginTop: 28 }}>{tone.sub}</p>

            <div style={{ display: "flex", gap: 10, marginTop: 36 }}>
              <button className="btn btn-brand" onClick={onBook}>Book a first visit</button>
              <a className="btn btn-ghost" href="tel:+12498000333">
                <span style={{ width: 6, height: 6, borderRadius: 999, background: "var(--ok)" }} />
                Call +1 249 800 0333
              </a>
            </div>

            <div className="hero-stats" style={{ display: "flex", gap: 40, marginTop: 64, flexWrap: "wrap" }}>
              <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
                <div className="stat-num">4</div>
                <div className="eyebrow" style={{ fontSize: 10 }}>clinics</div>
              </div>
              <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
                <div className="stat-num">11k+</div>
                <div className="eyebrow" style={{ fontSize: 10 }}>patients</div>
              </div>
              <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
                <div className="stat-num">&lt;3h</div>
                <div className="eyebrow" style={{ fontSize: 10 }}>median first reply</div>
              </div>
              <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
                <div className="stat-num" style={{ fontSize: 32, whiteSpace: "nowrap" }}>EN&nbsp;·&nbsp;FR-CA</div>
                <div className="eyebrow" style={{ fontSize: 10 }}>every channel</div>
              </div>
            </div>
          </div>

          <HeroLiveColumn onWhy={onWhy} />
        </div>
      </div>
    </section>
  );
}

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