// Shared UI: Nav, Footer, primitives

const { useState, useEffect, useRef, useMemo, useCallback } = React;

// ─────────────────────────────────────────────────────────────
// Reveal-on-scroll
// ─────────────────────────────────────────────────────────────
function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => { if (e.isIntersecting) { el.classList.add('in'); io.disconnect(); } });
    }, { threshold: 0.08 });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return ref;
}

function Reveal({ children, delay = 0, as: As = "div", ...rest }) {
  const ref = useReveal();
  return (
    <As ref={ref} className={"reveal " + (rest.className || "")} style={{ transitionDelay: delay + "ms", ...(rest.style || {}) }}>
      {children}
    </As>
  );
}

// ─────────────────────────────────────────────────────────────
// Brand mark
// ─────────────────────────────────────────────────────────────
function Brand({ onClick }) {
  return (
    <div className="brand" onClick={onClick}>
      <span className="mark">G</span>
      <span className="name">
        <b>GenTech</b>
        <small>Construction · Chattanooga</small>
      </span>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Top nav
// ─────────────────────────────────────────────────────────────
function Nav({ route, setRoute }) {
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const on = () => setScrolled(window.scrollY > 24);
    on();
    window.addEventListener("scroll", on, { passive: true });
    return () => window.removeEventListener("scroll", on);
  }, []);

  const items = [
    { id: "home", label: "Index" },
    { id: "projects", label: "Projects" },
    { id: "services", label: "Capabilities" },
    { id: "about", label: "Practice" },
    { id: "map", label: "Regions" },
  ];

  return (
    <nav className={"nav " + (scrolled ? "scrolled" : "")}>
      <Brand onClick={() => setRoute("home")} />
      <div className="nav-links">
        {items.map(i => (
          <a key={i.id}
             className={route === i.id ? "active" : ""}
             onClick={() => setRoute(i.id)}>{i.label}</a>
        ))}
      </div>
      <div className="nav-right">
        <span className="nav-meta">Chattanooga · TN</span>
        <button className="btn sm" onClick={() => setRoute("about")}>
          Start a Project <span className="arr">→</span>
        </button>
      </div>
    </nav>
  );
}

// ─────────────────────────────────────────────────────────────
// Footer
// ─────────────────────────────────────────────────────────────
function Footer({ setRoute }) {
  return (
    <footer className="footer">
      <div className="ft-top">
        <div>
          <div style={{ fontFamily: "var(--serif)", fontSize: 36, lineHeight: 1.05, marginBottom: 24, maxWidth: 460 }}>
            Planning a project on the mountain or in the city?<br />
            <span className="italic" style={{ color: "var(--bronze-2)" }}>Let's talk.</span>
          </div>
          <a className="lnk" onClick={() => setRoute("about")}>
            Start a Conversation →
          </a>
        </div>
        <div>
          <h6>Studio</h6>
          <ul>
            <li><a onClick={() => setRoute("about")}>Practice</a></li>
            <li><a onClick={() => setRoute("about")}>Leadership</a></li>
            <li><a>Careers</a></li>
            <li><a>Press</a></li>
          </ul>
        </div>
        <div>
          <h6>Work</h6>
          <ul>
            <li><a onClick={() => setRoute("projects")}>All Projects</a></li>
            <li><a onClick={() => setRoute("services")}>Capabilities</a></li>
            <li><a onClick={() => setRoute("map")}>Regions</a></li>
            <li><a onClick={() => setRoute("about")}>Practice</a></li>
          </ul>
        </div>
        <div>
          <h6>Studio</h6>
          <ul style={{ color: "var(--bone-2)" }}>
            <li>Chattanooga, Tennessee</li>
            <li>Serving TN · GA · AL</li>
            <li style={{ marginTop: 10 }}>By appointment</li>
            <li><a className="uline">hello@gentechconstruction.com</a></li>
          </ul>
        </div>
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 28, padding: "40px 0 28px", flexWrap: "wrap" }}>
        <div style={{ background: "var(--bone)", padding: "14px 22px", borderRadius: 2, display: "inline-flex" }}>
          <img src="assets/gentech-logo.png" alt="GenTech Construction" style={{ height: 38, display: "block" }} />
        </div>
        <div style={{ color: "var(--bone-3)", fontSize: 12, letterSpacing: ".06em", lineHeight: 1.6, maxWidth: 380 }}>
          The current GenTech identity. A refreshed mark and full brand system would accompany this site in production.
        </div>
      </div>
      <div className="ft-bot">
        <span>© GenTech Construction · Chattanooga, Tennessee</span>
        <span style={{ display: "flex", gap: 24 }}>
          <a className="uline">Privacy</a>
          <a className="uline">Accessibility</a>
          <a className="uline">Diversity Policy</a>
        </span>
      </div>
    </footer>
  );
}

// ─────────────────────────────────────────────────────────────
// Project tile (used in grids)
// ─────────────────────────────────────────────────────────────
function ProjectTile({ project, index, onClick, aspect = "ar-3-4", showNum = true }) {
  const [hover, setHover] = useState(false);
  return (
    <div className="tile" onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)} onClick={onClick}>
      <div className={"img tile-img " + aspect} style={{ backgroundImage: `url(${project.img})` }} />
      <div style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, rgba(15,16,12,.10) 0%, rgba(15,16,12,.0) 30%, rgba(15,16,12,.55) 100%)" }} />
      {showNum && <span className="ti-num">{String(index + 1).padStart(2, "0")} · {project.role}</span>}
      <span className="ti-cat">{project.category}</span>
      <div className="tile-meta">
        <div>
          <div className="eyebrow" style={{ color: "var(--bone-2)", marginBottom: 8 }}>{project.place}</div>
          <h3 style={{ color: "var(--bone)" }}>{project.title}</h3>
        </div>
        <div className="meta-r">
          <span className="lnk" style={{ borderColor: hover ? "var(--bronze)" : "var(--bone-4)", color: hover ? "var(--bronze-2)" : "var(--bone)" }}>View →</span>
        </div>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Stat block
// ─────────────────────────────────────────────────────────────
function Stat({ num, label }) {
  return (
    <div style={{ borderTop: "1px solid var(--line)", paddingTop: 18 }}>
      <div className="num" style={{ color: "var(--bone)" }}>{num}</div>
      <div style={{ marginTop: 14, color: "var(--bone-3)", fontSize: 12, letterSpacing: ".06em", maxWidth: 220, lineHeight: 1.4 }}>{label}</div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Marquee
// ─────────────────────────────────────────────────────────────
function Marquee({ items }) {
  const list = [...items, ...items, ...items];
  return (
    <div className="marquee">
      <div className="track">
        {list.map((it, i) => (
          <React.Fragment key={i}>
            <span style={{ color: "var(--bone)", whiteSpace: "nowrap" }} className="italic">{it}</span>
            <span className="dot">✦</span>
          </React.Fragment>
        ))}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────
// Section title
// ─────────────────────────────────────────────────────────────
function SectionTitle({ eyebrow, title, sub, right }) {
  return (
    <Reveal>
      <div className="s-title">
        <div>
          <div className="eyebrow" style={{ marginBottom: 22 }}><span className="dot"></span>{eyebrow}</div>
          <h2>{title}</h2>
        </div>
        <div className="s-sub">{sub}{right}</div>
      </div>
    </Reveal>
  );
}

// ─────────────────────────────────────────────────────────────
// Vertical side mark
// ─────────────────────────────────────────────────────────────
function SideMark({ children }) {
  return <div className="side-mark">{children}</div>;
}

Object.assign(window, {
  useReveal, Reveal, Brand, Nav, Footer, ProjectTile, Stat, Marquee, SectionTitle, SideMark
});
