/* ============================================== */
/* Shared chrome — TopBar, Header, Footer, Router */
/* ============================================== */

const { useState, useEffect, useRef } = React;

/* ---------- Scroll reveal observer ---------- */
function initReveal() {
  const els = document.querySelectorAll(
    '.ibd-reveal, .ibd-reveal-left, .ibd-reveal-right, .ibd-reveal-scale'
  );
  if (!els.length) return;
  const io = new IntersectionObserver((entries) => {
    entries.forEach(e => {
      if (e.isIntersecting) {
        e.target.classList.add('ibd-visible');
        io.unobserve(e.target);
      }
    });
  }, { threshold: 0.08, rootMargin: '0px 0px -40px 0px' });
  els.forEach(el => io.observe(el));
}

/* ---------- tiny hash router ---------- */
function useRoute() {
  const [route, setRoute] = useState(window.location.hash.replace("#", "") || "/");
  useEffect(() => {
    const on = () => setRoute(window.location.hash.replace("#", "") || "/");
    window.addEventListener("hashchange", on);
    return () => window.removeEventListener("hashchange", on);
  }, []);
  return [route, (r) => {window.location.hash = r;window.scrollTo({ top: 0, behavior: "instant" });}];
}

/* ---------- IBD logotype ---------- */
function Logo({ variant = "light", size = 38 }) {
  const sub = variant === "light" ? "#B0BECA" : "#7E8B96";
  return (
    <a href="#/" style={{ display: "inline-flex", alignItems: "center", gap: 10 }}>
      {/* Logo image — taille agrandie pour lisibilité */}
      <img
        src="assets/logo-icon-mono.png"
        alt="IBD Services"
        style={{
          width: size * 2.2,
          height: size * 1.4,
          objectFit: "contain",
          objectPosition: "left center",
          display: "block",
          filter: "brightness(0) saturate(100%) invert(47%) sepia(98%) saturate(1200%) hue-rotate(10deg) brightness(103%) contrast(101%)",
        }} />
      <span style={{ display: "inline-flex", flexDirection: "column", lineHeight: 1 }}>
      </span>
    </a>
  );
}

/* ---------- Icônes sociales SVG ---------- */
function IconFacebook() {
  return (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
      <path d="M18 2h-3a5 5 0 0 0-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 0 1 1-1h3z"/>
    </svg>
  );
}
function IconInstagram() {
  return (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <rect x="2" y="2" width="20" height="20" rx="5" ry="5"/>
      <path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z"/>
      <line x1="17.5" y1="6.5" x2="17.51" y2="6.5"/>
    </svg>
  );
}
function IconWhatsApp() {
  return (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
      <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 0 1-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 0 1-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 0 1 2.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0 0 12.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 0 0 5.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 0 0-3.48-8.413z"/>
    </svg>
  );
}

/* ---------- Header ---------- */
function Header({ route, go }) {
  const items = [
    { label: "Accueil", path: "/" },
    { label: "Tarifs & Réservation", path: "/reservation" },
    { label: "À propos", path: "/a-propos" },
    { label: "FAQ", path: "/faq" },
    { label: "Contact", path: "/contact" },
  ];

  const [mobile, setMobile] = useState(false);
  return (
    <header style={{ background: "var(--marin)", borderBottom: "1px solid #14294A", position: "sticky", top: 0, zIndex: 50 }}>
      <div className="wrap" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", height: 78 }}>
        <Logo variant="light" />

        <nav className="ibd-nav" style={{ display: "flex", gap: 4 }}>
          {items.map((it) => {
            const active = route === it.path;
            return (
              <a key={it.path} href={"#" + it.path}
                onClick={() => window.scrollTo({ top: 0 })}
                style={{
                  padding: "10px 16px",
                  fontFamily: "var(--ff-body)", fontWeight: 600, fontSize: 13,
                  textTransform: "uppercase", letterSpacing: 1.5,
                  color: active ? "#fff" : "#B0BECA",
                  position: "relative", transition: "color .18s ease-out"
                }}>
                {it.label}
                {active && <span style={{ position: "absolute", left: 16, right: 16, bottom: 4, height: 2, background: "var(--orange)" }} />}
              </a>
            );
          })}
        </nav>

        <div className="ibd-header-cta" style={{ display: "flex", gap: 10, alignItems: "center" }}>
          <a href="#/reservation" className="btn btn--orange btn--sm">
            Réserver <span className="arr">→</span>
          </a>
        </div>

        <button className="ibd-burger" onClick={() => setMobile(true)} aria-label="Menu"
          style={{ display: "none", width: 44, height: 44, border: "1px solid #14294A", color: "#fff", alignItems: "center", justifyContent: "center" }}>
          <span style={{ display: "inline-flex", flexDirection: "column", gap: 4 }}>
            <span style={{ width: 22, height: 2, background: "#fff" }} />
            <span style={{ width: 22, height: 2, background: "#fff" }} />
            <span style={{ width: 22, height: 2, background: "#fff" }} />
          </span>
        </button>
      </div>

      {mobile && (
        <div style={{ position: "fixed", inset: 0, background: "var(--marin-deep)", zIndex: 60, padding: "28px 24px", display: "flex", flexDirection: "column" }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
            <Logo variant="light" />
            <button onClick={() => setMobile(false)} style={{ color: "#fff", fontSize: 28, background: "none", border: "none", cursor: "pointer" }}>×</button>
          </div>
          <nav style={{ marginTop: 40, display: "flex", flexDirection: "column", gap: 4 }}>
            {items.map((it) => (
              <a key={it.path} href={"#" + it.path}
                onClick={() => setMobile(false)}
                className="h2"
                style={{ color: route === it.path ? "var(--orange)" : "#fff", fontSize: 38, padding: "12px 0", borderBottom: "1px solid #14294A" }}>
                {it.label}
              </a>
            ))}
          </nav>
        </div>
      )}
    </header>
  );
}

/* ---------- Footer ---------- */
function Footer() {
  const socials = [
    { icon: <IconFacebook />, href: "#", label: "Facebook" },
    { icon: <IconInstagram />, href: "#", label: "Instagram" },
    { icon: <IconWhatsApp />, href: "https://wa.me/221789230600", label: "WhatsApp" },
  ];

  return (
    <footer style={{ background: "var(--marin-deep)", color: "var(--acier)" }}>

      <div className="wrap" style={{ padding: "28px 32px 20px" }}>
        <div className="ibd-footer-grid" style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr 1fr 1fr", gap: 32 }}>
          <div>
            <Logo variant="light" size={42} />
            <p style={{ marginTop: 22, color: "var(--sable)", lineHeight: 1.4, maxWidth: 320, fontFamily: "var(--ff-title)", fontWeight: 700, fontSize: 22, letterSpacing: -.3, textTransform: "uppercase" }}>
              Vos chantiers<br/>commencent ici.
            </p>
            <p style={{ marginTop: 14, color: "var(--acier)", lineHeight: 1.6, maxWidth: 320, fontSize: 14 }}>
              Spécialiste de la location d'étais métalliques sur la Petite Côte.
              Stock professionnel, livraison rapide, équipe de terrain.
            </p>

            {/* Icônes sociales avec vrais logos */}
            <div style={{ marginTop: 24, display: "flex", gap: 8 }}>
              {socials.map((s) => (
                <a key={s.label} href={s.href}
                  aria-label={s.label}
                  target={s.href.startsWith("http") ? "_blank" : undefined}
                  rel="noopener noreferrer"
                  style={{
                    width: 38, height: 38,
                    display: "inline-flex", alignItems: "center", justifyContent: "center",
                    border: "1px solid #14294A",
                    color: "#fff",
                    transition: "border-color .2s, color .2s, background .2s",
                  }}
                  onMouseEnter={e => { e.currentTarget.style.borderColor = "var(--orange)"; e.currentTarget.style.color = "var(--orange)"; }}
                  onMouseLeave={e => { e.currentTarget.style.borderColor = "#14294A"; e.currentTarget.style.color = "#fff"; }}
                >
                  {s.icon}
                </a>
              ))}
            </div>
          </div>

          <FooterCol title="Navigation" links={[
            ["Accueil", "#/"],
            ["Tarifs & Réservation", "#/reservation"],
            ["À propos", "#/a-propos"],
            ["FAQ", "#/faq"],
            ["Contact", "#/contact"],
          ]} />

          <FooterCol title="Services" links={[
            ["Étais standard", "#/reservation"],
            ["Camion plateau", "#/reservation"],
            ["Forfait Pro", "#/reservation"],
            ["Devis sur mesure", "#/contact"],
          ]} />

          <div>
            <h4 className="label" style={{ color: "#fff", marginBottom: 18, fontSize: 11 }}>Contact</h4>
            <p style={{ lineHeight: 1.7, color: "var(--acier)", fontSize: 14 }}>
              Route de Saly<br/>
              Mbour, Sénégal<br/>

              <a href="tel:+221789230600" style={{ color: "#fff" }}>+221 78 923 06 00</a><br/>
              <a href="mailto:contact@ibdservices.pro" style={{ color: "#fff" }}>contact@ibdservices.pro</a>
            </p>

            <h4 className="label" style={{ color: "#fff", margin: "24px 0 12px", fontSize: 11 }}>Paiement</h4>
            <div style={{ display: "flex", flexWrap: "wrap", gap: 6, fontFamily: "var(--ff-mono)", fontSize: 10, textTransform: "uppercase", letterSpacing: 1.5 }}>
              <PayChip label="Wave" />
              <PayChip label="Orange Money" />
              <PayChip label="Virement" />
            </div>
          </div>
        </div>

        <div style={{
          marginTop: 56, paddingTop: 24,
          borderTop: "1px solid #14294A",
          display: "flex", justifyContent: "space-between", flexWrap: "wrap", gap: 12,
          color: "#7E8B96", fontFamily: "var(--ff-mono)", fontSize: 11, letterSpacing: 1.5, textTransform: "uppercase"
        }}>
          <span>© 2026 IBD Services — Tous droits réservés</span>
          <span>Mbour · Saly · Sénégal</span>
        </div>
      </div>
    </footer>
  );
}

function FooterCol({ title, links }) {
  return (
    <div>
      <h4 className="label" style={{ color: "#fff", marginBottom: 18, fontSize: 11 }}>{title}</h4>
      <ul style={{ listStyle: "none", display: "flex", flexDirection: "column", gap: 10 }}>
        {links.map(([l, h]) => (
          <li key={l}>
            <a href={h} style={{ color: "var(--acier)", transition: "color .18s", fontSize: 14 }}
              onMouseEnter={e => e.currentTarget.style.color = "#fff"}
              onMouseLeave={e => e.currentTarget.style.color = "var(--acier)"}>
              {l}
            </a>
          </li>
        ))}
      </ul>
    </div>
  );
}

function PayChip({ label }) {
  return (
    <span style={{ padding: "6px 10px", border: "1px solid #14294A", color: "#fff" }}>{label}</span>
  );
}

/* ---------- Page transition wrapper ---------- */
function Page({ children }) {
  useEffect(() => {
    // Small delay to let React finish rendering before observing
    const t = setTimeout(initReveal, 60);
    return () => clearTimeout(t);
  }, []);
  return <main style={{ minHeight: "60vh", animation: "ibd-fade .35s ease-out" }}>{children}</main>;
}

/* expose */
Object.assign(window, { useRoute, Header, Footer, Logo, Page });
