// components.jsx — shared UI: Logo, Nav, Footer, primitives

const { useState, useEffect, useRef } = React;

/* ─────────── Logo ─────────────────────────────────────────────────────── */

function Logo({ scale = 1, onClick }) {
  const h = 96 * scale;
  return (
    <a className="logo-link logo-img-link" onClick={onClick} href="#/" aria-label="URäume — Räume für Unternehmen">
      <img
        className="logo-img logo-light"
        src="assets/logo.png"
        alt="URäume — Räume für Unternehmen"
        style={{ height: h, width: "auto" }}
      />
      <img
        className="logo-img logo-dark"
        src="assets/logo-dark.png"
        alt=""
        aria-hidden="true"
        style={{ height: h, width: "auto" }}
      />
    </a>
  );
}

/* ─────────── Nav ──────────────────────────────────────────────────────── */

function Nav({ route, onNavigate }) {
  const [open, setOpen] = useState(false);
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 12);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  // Kontakt-only: subtle scroll-driven logo growth.
  // Range = one viewport height (so it feels paced the same on mobile and desktop).
  // Max scale: 1.30 on desktop, 1.18 on mobile (where header height is the bottleneck).
  useEffect(() => {
    if (route !== "kontakt") {
      document.documentElement.style.setProperty("--logo-scale", "1");
      return;
    }
    const compute = () => {
      const y = window.scrollY;
      const vh = window.innerHeight || 800;
      const isMobile = window.innerWidth < 720;
      const maxScale = isMobile ? 1.22 : 1.38;
      const range = vh;
      const s = 1 + Math.min(y, range) / range * (maxScale - 1);
      document.documentElement.style.setProperty("--logo-scale", s.toFixed(4));
    };
    compute();
    window.addEventListener("scroll", compute, { passive: true });
    window.addEventListener("resize", compute);
    return () => {
      window.removeEventListener("scroll", compute);
      window.removeEventListener("resize", compute);
      document.documentElement.style.setProperty("--logo-scale", "1");
    };
  }, [route]);

  useEffect(() => { setOpen(false); }, [route]);

  const links = [
    { id: "standorte", label: "Standorte" },
    { id: "barsbuettel", label: "Barsbüttel", indent: true },
    { id: "ahrensburg", label: "Ahrensburg", indent: true },
    { id: "kontakt", label: "Kontakt" },
  ];

  const activeTop = route === "barsbuettel" || route === "ahrensburg" ? "standorte" : route;

  return (
    <header className={"nav " + (scrolled ? "is-scrolled" : "")}>
      <div className="wrap nav-row">
        <Logo onClick={(e) => { e.preventDefault(); onNavigate("standorte"); }} />
        <nav className="nav-links" aria-label="Hauptnavigation">
          <a
            href="#/standorte"
            onClick={(e) => { e.preventDefault(); onNavigate("standorte"); }}
            className={"nav-link " + (activeTop === "standorte" ? "is-active" : "")}
          >Standorte</a>
          <a
            href="#/kontakt"
            onClick={(e) => { e.preventDefault(); onNavigate("kontakt"); }}
            className={"nav-link " + (activeTop === "kontakt" ? "is-active" : "")}
          >Kontakt</a>
        </nav>
        <button
          className={"nav-toggle " + (open ? "is-open" : "")}
          aria-label="Menü"
          aria-expanded={open}
          onClick={() => setOpen((o) => !o)}
        >
          <span></span><span></span>
        </button>
      </div>
      {open && (
        <div className="nav-drawer">
          {links.map((l) => (
            <a
              key={l.id}
              href={"#/" + l.id}
              onClick={(e) => { e.preventDefault(); onNavigate(l.id); }}
              className={"nav-drawer-link " + (l.indent ? "is-indent " : "") + (route === l.id ? "is-active" : "")}
            >
              <span>{l.label}</span>
              <span className="arr">→</span>
            </a>
          ))}
        </div>
      )}
    </header>
  );
}

/* ─────────── Footer ───────────────────────────────────────────────────── */

function Footer({ onNavigate }) {
  const year = new Date().getFullYear();
  return (
    <footer className="footer">
      <div className="wrap footer-grid">
        <div className="footer-col">
          <div className="footer-h">Standorte</div>
          <a href="#/barsbuettel" onClick={(e)=>{e.preventDefault();onNavigate("barsbuettel");}}>Barsbüttel</a>
          <a href="#/ahrensburg" onClick={(e)=>{e.preventDefault();onNavigate("ahrensburg");}}>Ahrensburg</a>
        </div>
        <div className="footer-col">
          <div className="footer-h">Kontakt</div>
          <a href="tel:+4945324084744">+49 4532 40847 44</a>
          <a href="mailto:info@uraeume.de">info@uraeume.de</a>
        </div>
        <div className="footer-col">
          <div className="footer-h">URäume GmbH</div>
          <div className="footer-addr">
            Ahrensburger Straße 3<br />
            22941 Hammoor
          </div>
        </div>
      </div>
      <div className="wrap footer-bottom">
        <div className="copyright">© {year} URäume GmbH</div>
        <div className="legal">
          <a href="#/impressum" onClick={(e)=>{e.preventDefault();onNavigate("impressum");}}>Impressum</a>
          <span aria-hidden="true">·</span>
          <a href="#/datenschutz" onClick={(e)=>{e.preventDefault();onNavigate("datenschutz");}}>Datenschutzerklärung</a>
        </div>
      </div>
    </footer>
  );
}

/* ─────────── Placeholders for imagery ─────────────────────────────────── */

function ImagePlaceholder({ label, ratio = "4 / 3", tone = "default", src, alt, objectPosition }) {
  if (src) {
    return (
      <div className="img-real" style={{ aspectRatio: ratio }}>
        <img src={src} alt={alt || label || ""} loading="lazy" style={{ objectPosition: objectPosition || "center" }} />
      </div>
    );
  }
  return (
    <div className={"img-ph tone-" + tone} style={{ aspectRatio: ratio }}>
      <svg className="img-ph-svg" preserveAspectRatio="none" viewBox="0 0 100 100">
        <defs>
          <pattern id="ph-stripes" patternUnits="userSpaceOnUse" width="8" height="8" patternTransform="rotate(35)">
            <line x1="0" y1="0" x2="0" y2="8" stroke="currentColor" strokeWidth="0.6" opacity="0.18" />
          </pattern>
        </defs>
        <rect width="100" height="100" fill="url(#ph-stripes)" />
      </svg>
      <div className="img-ph-label">{label}</div>
    </div>
  );
}

/* ─────────── Small icons (line, restrained) ───────────────────────────── */

const Icon = {
  arrow: () => (<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" strokeWidth="1.25" strokeLinecap="square" /></svg>),
  pin:   () => (<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true"><path d="M7 1.5c-2.5 0-4.5 2-4.5 4.5 0 3.4 4.5 6.5 4.5 6.5s4.5-3.1 4.5-6.5c0-2.5-2-4.5-4.5-4.5z" stroke="currentColor" strokeWidth="1.1" /><circle cx="7" cy="6" r="1.4" stroke="currentColor" strokeWidth="1.1" /></svg>),
  ruler: () => (<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true"><rect x="1.5" y="4.5" width="11" height="5" stroke="currentColor" strokeWidth="1.1" /><path d="M3.5 4.5v2M5.5 4.5v3M7.5 4.5v2M9.5 4.5v3M11.5 4.5v2" stroke="currentColor" strokeWidth="1.1" /></svg>),
  bolt:  () => (<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true"><path d="M8 1.5L3 8h3.5L6 12.5 11 6H7.5L8 1.5z" stroke="currentColor" strokeWidth="1.1" strokeLinejoin="round" /></svg>),
  car:   () => (<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true"><path d="M2 9V7l1.5-3h7L12 7v2M2 9h10M2 9v1.5M12 9v1.5" stroke="currentColor" strokeWidth="1.1" /><circle cx="4.5" cy="10" r="1" stroke="currentColor" strokeWidth="1.1" /><circle cx="9.5" cy="10" r="1" stroke="currentColor" strokeWidth="1.1" /></svg>),
  sun:   () => (<svg width="14" height="14" viewBox="0 0 14 14" fill="none" aria-hidden="true"><circle cx="7" cy="7" r="2.5" stroke="currentColor" strokeWidth="1.1" /><path d="M7 1v1.5M7 11.5V13M1 7h1.5M11.5 7H13M2.7 2.7l1 1M10.3 10.3l1 1M11.3 2.7l-1 1M3.7 10.3l-1 1" stroke="currentColor" strokeWidth="1.1" /></svg>),
};

Object.assign(window, { Logo, Nav, Footer, ImagePlaceholder, Icon });
