/* global React, WIIN_COPY */
const { useState, useEffect, useRef } = React;

/* ---------------- Header / Logo / Footer ---------------- */

/**
 * Renders the official WIIN Canada logo as an <img>.
 * - Looks for `wiin-logo.png` (and a few common variants) at the project root.
 * - If the file is missing, falls back to a brand-aligned mark so the page never
 *   shows a broken-image icon. Drop the official PNG in the project root and
 *   it will render automatically.
 */
/**
 * Renders the official WIIN Canada logo as an <img>.
 * - Loads `wiin-logo.svg` at the project root (drop your asset there to override
 *   the placeholder we ship).
 * - If the file is missing, falls back to a brand-aligned mark so the page
 *   never shows a broken-image icon.
 */
function Logo({ onClick, compact }) {
  const [missing, setMissing] = useState(false);
  return (
    <a className={compact ? "logo-mark logo-mark-footer" : "logo-mark"} onClick={onClick} aria-label="WIIN Canada — accueil">
      {missing ? (
        <span className="logo-fallback" aria-hidden="true">W</span>
      ) : (
        <img
          className="logo-img"
          src="wiin-logo.svg"
          alt="WIIN Canada"
          onError={() => setMissing(true)}
          style={compact ? { height: 112 } : null}
        />
      )}
    </a>
  );
}

function LangToggle({ lang, setLang }) {
  return (
    <div className="lang-toggle" role="group" aria-label="Language">
      <button
        className={lang === "fr" ? "active" : ""}
        onClick={() => setLang("fr")}
        aria-pressed={lang === "fr"}
      >FR</button>
      <button
        className={lang === "en" ? "active" : ""}
        onClick={() => setLang("en")}
        aria-pressed={lang === "en"}
      >EN</button>
    </div>
  );
}

function Header({ lang, setLang, route, go }) {
  const c = WIIN_COPY[lang].nav;
  const link = (key, label) => (
    <a className={route === key ? "active" : ""} onClick={() => go(key)}>{label}</a>
  );
  return (
    <header className="site" role="banner">
      <div className="wrap row">
        <Logo onClick={() => go("home")} />
        <nav role="navigation" aria-label="Primary">
          {link("mission", c.mission)}
          {link("pillars", c.pillars)}
          <a href="https://luma.com/n3ebt1yb" target="_blank" rel="noopener noreferrer"
            aria-label={lang === "fr" ? "JCM 2026 — billetterie sur Luma (ouvre un nouvel onglet)" : "JCM 2026 — tickets on Luma (opens a new tab)"}>
            JCM 2026 ↗
          </a>
          {link("gouvernance", lang === "fr" ? "Gouvernance" : "Governance")}
          {link("contact", c.contact)}
        </nav>
        <div style={{ display: "flex", gap: 14, alignItems: "center" }}>
          <LangToggle lang={lang} setLang={setLang} />
          <button className="btn btn-solid" onClick={() => go("contact")} style={{ padding: "10px 18px", minHeight: 40, fontSize: 14 }}>
            {c.cta} →
          </button>
        </div>
      </div>
    </header>
  );
}

function Footer({ lang, go }) {
  const c = WIIN_COPY[lang].home;
  const nav = WIIN_COPY[lang].nav;
  return (
    <footer className="site" role="contentinfo">
      <div className="wrap">
        <div className="grid-footer">
          <div>
            <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 18 }}>
              <Logo compact onClick={() => go("home")} />
            </div>
            <p style={{ margin: "0 0 14px", lineHeight: 1.6, maxWidth: 360, fontFamily: "'Playfair Display', Georgia, serif", fontStyle: "italic", fontSize: 16, color: "rgba(255,255,255,0.88)" }}>
              {c.footerTagline}
            </p>
            <p className="muted" style={{ fontSize: 13, color: "rgba(255,255,255,0.5)", margin: 0 }}>
              {c.footerAddress}
            </p>
          </div>

          <div>
            <h4>{lang === "fr" ? "Naviguer" : "Navigate"}</h4>
            <ul>
              <li><a onClick={() => go("mission")}>{nav.mission}</a></li>
              <li><a onClick={() => go("pillars")}>{nav.pillars}</a></li>
              <li><a onClick={() => go("gouvernance")}>{lang === "fr" ? "Gouvernance" : "Governance"}</a></li>
            </ul>
          </div>

          <div>
            <h4>{lang === "fr" ? "S'impliquer" : "Get involved"}</h4>
            <ul>
              <li><a onClick={() => go("contact")}>{lang === "fr" ? "Bénévoler" : "Volunteer"}</a></li>
              <li><a onClick={() => go("contact")}>{lang === "fr" ? "Commanditer" : "Sponsor"}</a></li>
              <li><a onClick={() => go("contact")}>{lang === "fr" ? "Devenir bailleur" : "Become a funder"}</a></li>
              <li><a onClick={() => go("contact")}>{nav.contact}</a></li>
            </ul>
          </div>

          <div>
            <h4>{lang === "fr" ? "Légal" : "Legal"}</h4>
            <ul>
              {c.footerLegal.map((l, i) => <li key={i}><a onClick={() => go("confidentialite")} style={{ cursor: "pointer" }}>{l}</a></li>)}
            </ul>
          </div>
        </div>

        {/* Transparency block — S141 v23.2 (D-V23.2-PUB-01 reversal). Legal identity + governance link + contact. */}
        <div className="transparency-block" style={{
          paddingTop: 18,
          paddingBottom: 14,
          marginTop: 16,
          borderTop: "1px solid rgba(255,255,255,0.10)",
          display: "flex",
          flexWrap: "wrap",
          gap: 18,
          alignItems: "center",
          justifyContent: "center",
          fontFamily: "'Inter', sans-serif",
          fontSize: 12.5,
          color: "rgba(255,255,255,0.72)",
          textAlign: "center",
        }}>
          <span><strong style={{ color: "rgba(255,255,255,0.92)" }}>OBNL fédéral</strong> · {lang === "fr" ? "Loi canadienne sur les organisations à but non lucratif" : "Canada Not-for-profit Corporations Act"}</span>
          <span aria-hidden="true" style={{ opacity: 0.4 }}>·</span>
          <a onClick={() => go("gouvernance")} style={{ color: "rgba(255,255,255,0.92)", textDecoration: "underline", textUnderlineOffset: "2px", cursor: "pointer" }}>{lang === "fr" ? "Gouvernance" : "Governance"}</a>
          <span aria-hidden="true" style={{ opacity: 0.4 }}>·</span>
          <a href="mailto:info@wiincanada.org" style={{ color: "rgba(255,255,255,0.92)", textDecoration: "underline", textUnderlineOffset: "2px" }}>info@wiincanada.org</a>
        </div>

        <div className="legal">
          <span>{lang === "fr" ? "© 2026 Réseau WIIN Canada. Tous droits réservés." : "© 2026 Réseau WIIN Canada. All rights reserved."}</span>
          <span className="attrib">
            {c.footerAttribLink && c.footerAttribURL
              ? c.footerAttrib.split(c.footerAttribLink).map((part, i, arr) => (
                  <React.Fragment key={i}>
                    {part}
                    {i < arr.length - 1 && (
                      <a
                        href={c.footerAttribURL}
                        target="_blank"
                        rel="noopener noreferrer"
                        aria-label={`${c.footerAttribLink} — ${(WIIN_COPY[lang].locale === "FR" ? "ouvre dans un nouvel onglet" : "opens in a new tab")}`}
                        style={{ color: "inherit", textDecoration: "underline", textUnderlineOffset: "2px" }}
                      >
                        {c.footerAttribLink}
                      </a>
                    )}
                  </React.Fragment>
                ))
              : c.footerAttrib}
          </span>
        </div>
      </div>
    </footer>
  );
}

window.Logo = Logo;
window.LangToggle = LangToggle;
window.WIINHeader = Header;
window.WIINFooter = Footer;
