/* global React, BrandMark, WaveDivider, IconPhone, IconClose, IconMapPin, IconMail */
const { useState, useEffect } = React;

const NAV_ITEMS = [
  { id: "home", label: "Home" },
  { id: "residences", label: "Residences" },
  { id: "amenities", label: "Amenities" },
  { id: "gallery", label: "Gallery" },
  { id: "location", label: "Location" },
  { id: "contact", label: "Contact" },
];

function Header({ page, navTo, openTour, showPreLeasing }) {
  return (
    <>
      {showPreLeasing && (
        <div className="preleasing-banner">
          <span><b>Pre-Leasing Now</b> — First Residents Move In Summer 2026 · Join the waitlist for priority access</span>
        </div>
      )}
      <div className="topbar">
        <div className="topbar-inner">
          <span>Oceanfront Living</span>
          <span className="dot">◆</span>
          <span>Enduring Quality</span>
          <span className="dot">◆</span>
          <span>Iconic Location</span>
        </div>
      </div>
      <header className="nav">
        <div className="nav-inner">
          <a href="#home" className="brand-link" onClick={(e) => { e.preventDefault(); navTo("home"); }}>
            <BrandMark size={42} />
            <div className="brand-text">
              <div className="name">Boardwalk House</div>
              <div className="tag">Atlantic City · Oceanfront Apartments</div>
            </div>
          </a>
          <nav className="nav-links">
            {NAV_ITEMS.map((it) => (
              <a key={it.id}
                 href={"#" + it.id}
                 className={"nav-link" + (page === it.id ? " active" : "")}
                 onClick={(e) => { e.preventDefault(); navTo(it.id); }}>
                {it.label}
              </a>
            ))}
          </nav>
          <div className="nav-cta">
            <a className="btn btn-primary btn-sm" href="#contact" onClick={(e) => { e.preventDefault(); openTour(); }}>
              Schedule a Tour
            </a>
          </div>
        </div>
      </header>
    </>
  );
}

function Footer({ navTo, onWaitlistSubmit }) {
  const [email, setEmail] = useState("");
  const [submitted, setSubmitted] = useState(false);
  const submit = async (e) => {
    e.preventDefault();
    if (!email.includes("@")) return;
    try {
      await window.submitForm({
        subject: "[Boardwalk House] Waitlist signup — footer",
        formType: "waitlist",
        source: "footer",
        email,
      });
    } catch (_) { /* silent — UI still confirms */ }
    onWaitlistSubmit && onWaitlistSubmit(email);
    setSubmitted(true);
    setEmail("");
    setTimeout(() => setSubmitted(false), 4000);
  };
  return (
    <footer className="footer">
      <div className="shell">
        <div className="footer-grid">
          <div>
            <div style={{ display: "flex", alignItems: "center", gap: 14, marginBottom: 16 }}>
              <BrandMark size={48} />
              <div>
                <div style={{ fontFamily: "var(--serif)", fontSize: 24, fontWeight: 500, color: "var(--linen-2)" }}>
                  Boardwalk House
                </div>
                <div style={{ fontFamily: "var(--sans)", fontSize: 9.5, letterSpacing: "0.32em", textTransform: "uppercase", color: "var(--bronze-soft)", marginTop: 4 }}>
                  Atlantic City
                </div>
              </div>
            </div>
            <p style={{ fontSize: 13, color: "rgba(245,242,237,0.7)", lineHeight: 1.6, maxWidth: 320 }}>
              Forty-seven oceanfront residences on the Atlantic City Boardwalk. Built 1984. Renovated 2026.
            </p>
            <div style={{ marginTop: 20, display: "flex", flexDirection: "column", gap: 10, fontSize: 13, color: "rgba(245,242,237,0.78)" }}>
              <div style={{ display: "flex", gap: 10, alignItems: "flex-start" }}>
                <IconMapPin size={16} style={{ color: "var(--bronze-soft)", marginTop: 2 }} />
                <span>190 S Kentucky Ave<br/>Atlantic City, NJ 08401</span>
              </div>
              <div style={{ display: "flex", gap: 10, alignItems: "center" }}>
                <IconPhone size={16} style={{ color: "var(--bronze-soft)" }} />
                <a href="tel:6097107977" style={{ textDecoration: "none", color: "rgba(245,242,237,0.9)" }}>
                  Call or Text (609) 710-7977
                </a>
              </div>
              <div style={{ display: "flex", gap: 10, alignItems: "center" }}>
                <IconMail size={16} style={{ color: "var(--bronze-soft)" }} />
                <a href="mailto:leasing@boardwalkhouseac.com" style={{ textDecoration: "none", color: "rgba(245,242,237,0.9)" }}>
                  leasing@boardwalkhouseac.com
                </a>
              </div>
            </div>
          </div>
          <div>
            <h4>Site Map</h4>
            <ul className="footer-list">
              {NAV_ITEMS.map((it) => (
                <li key={it.id}>
                  <a href={"#" + it.id} onClick={(e) => { e.preventDefault(); navTo(it.id); }}>{it.label}</a>
                </li>
              ))}
            </ul>
          </div>
          <div>
            <h4>Programs</h4>
            <ul className="footer-list">
              <li><a href="#contact" onClick={(e) => { e.preventDefault(); navTo("contact"); }}>Schedule a Tour</a></li>
              <li><a href="#contact" onClick={(e) => { e.preventDefault(); navTo("contact"); }}>Apply Now</a></li>
              <li><a href="#contact" onClick={(e) => { e.preventDefault(); navTo("contact"); }}>Broker Referral Program</a></li>
              <li><a href="#contact" onClick={(e) => { e.preventDefault(); navTo("contact"); }}>Preferred Employer Program</a></li>
              <li><a href="#contact" onClick={(e) => { e.preventDefault(); navTo("contact"); }}>Launch Offers</a></li>
            </ul>
          </div>
          <div>
            <h4>Waitlist · Summer 2026</h4>
            <p style={{ fontSize: 13, color: "rgba(245,242,237,0.7)", margin: "0 0 14px" }}>
              Be first to tour, first to apply, and first in line for launch offers.
            </p>
            <form onSubmit={submit} style={{ display: "grid", gap: 10 }}>
              <input
                type="email"
                placeholder="your@email.com"
                value={email}
                onChange={(e) => setEmail(e.target.value)}
                required
                style={{
                  background: "rgba(245,242,237,0.06)",
                  border: "1px solid rgba(245,242,237,0.3)",
                  color: "var(--linen-2)",
                  padding: "12px 14px",
                  fontFamily: "var(--sans)",
                  fontSize: 14,
                  borderRadius: 2,
                }}
              />
              <button type="submit" className="btn btn-primary btn-sm">
                {submitted ? "✓ You're on the list" : "Join the Waitlist"}
              </button>
            </form>
          </div>
        </div>
        <div className="footer-meta">
          <div style={{ display: "flex", gap: 14, alignItems: "center", flexWrap: "wrap" }}>
            <span>© 2026 Boardwalk House Atlantic City</span>
            <span style={{ opacity: 0.4 }}>·</span>
            <a href="#" style={{ textDecoration: "none", color: "inherit" }}>Privacy</a>
            <a href="#" style={{ textDecoration: "none", color: "inherit" }}>Terms</a>
            <a href="#" style={{ textDecoration: "none", color: "inherit" }}>Accessibility</a>
          </div>
          <div className="eho-badge">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4">
              <path d="M12 3l9 8h-2v9H5v-9H3l9-8z"/>
              <path d="M9 13h6M12 13v5"/>
              <path d="M9 16h6"/>
            </svg>
            <span>Equal Housing Opportunity</span>
          </div>
        </div>
      </div>
    </footer>
  );
}

function Modal({ open, onClose, title, eyebrow, children, wide }) {
  useEffect(() => {
    if (!open) return;
    const onKey = (e) => { if (e.key === "Escape") onClose(); };
    document.addEventListener("keydown", onKey);
    return () => document.removeEventListener("keydown", onKey);
  }, [open, onClose]);
  return (
    <div className={"modal-backdrop" + (open ? " open" : "")} onClick={onClose}>
      <div
        className="modal"
        style={wide ? { maxWidth: 820 } : undefined}
        onClick={(e) => e.stopPropagation()}>
        <button className="modal-close" onClick={onClose} aria-label="Close">
          <IconClose size={16} />
        </button>
        <div className="modal-head">
          {eyebrow && <div className="eyebrow" style={{ marginBottom: 8 }}>{eyebrow}</div>}
          <h3 style={{ fontSize: 30, margin: 0 }}>{title}</h3>
        </div>
        <div className="modal-body">{children}</div>
      </div>
    </div>
  );
}

// Section header used on every page
function SectionHead({ eyebrow, title, lede, light }) {
  return (
    <div className="section-head">
      {eyebrow && <span className={"eyebrow" + (light ? " on-dark" : "")}>{eyebrow}</span>}
      <h2 style={light ? { color: "var(--linen-2)" } : undefined}>{title}</h2>
      <WaveDivider onDark={light} />
      {lede && <p className="lede" style={light ? { color: "rgba(245,242,237,0.78)" } : undefined}>{lede}</p>}
    </div>
  );
}

// Brand strip (the "Oceanfront Living • Enduring Quality" repeating motif)
function BrandStrip({ items }) {
  const defaults = ["Oceanfront Living.", "Enduring Quality.", "Iconic Location.", "Made for the life you've earned."];
  const list = items || defaults;
  return (
    <div className="brand-strip">
      {list.map((t, i) => (
        <React.Fragment key={i}>
          {i > 0 && <span className="dot">◆</span>}
          <span>{t}</span>
        </React.Fragment>
      ))}
    </div>
  );
}

// Page hero used on sub-pages
function PageHero({ eyebrow, title, lede, photoUrl, children }) {
  return (
    <div className="page-hero">
      {photoUrl && <div className="photo" style={{ backgroundImage: `url(${photoUrl})` }} />}
      <div className="ovr" />
      <div className="inner">
        {eyebrow && <span className="eyebrow on-dark" style={{ display: "block", marginBottom: 18 }}>{eyebrow}</span>}
        <h1>{title}</h1>
        <WaveDivider onDark />
        {lede && <p className="lede" style={{ color: "rgba(245,242,237,0.85)", marginTop: 18 }}>{lede}</p>}
        {children}
      </div>
    </div>
  );
}

Object.assign(window, { Header, Footer, Modal, SectionHead, BrandStrip, PageHero, NAV_ITEMS });
