// components.jsx — shared UI: Nav, Footer, scenery, car illustrations

const Nav = ({ transparent, onLogo, onSelectCar }) => {
  const [openMenu, setOpenMenu] = React.useState(null);
  React.useEffect(() => {
    const close = () => setOpenMenu(null);
    document.addEventListener("click", close);
    return () => document.removeEventListener("click", close);
  }, []);
  const toggle = (k, e) => { e.stopPropagation(); setOpenMenu(openMenu === k ? null : k); };
  const pickCar = (c, e) => { e.stopPropagation(); setOpenMenu(null); if (onSelectCar) onSelectCar(c); };
  return (
    <nav className={`nav ${transparent ? "nav--transparent" : ""}`}>
      <a className="brand" onClick={onLogo} style={{cursor:"pointer"}}>
        <img src="assets/784-logo.webp" alt="784 Motors" className="brand-logo" />
      </a>
      <div className="nav-links">
        <div className={`nav-dropdown ${openMenu === "fleet" ? "is-open" : ""}`}>
          <a className="nav-dropdown-trigger" onClick={(e) => toggle("fleet", e)}>
            Fleet <Icon name="chevron-down" size={12} />
          </a>
          {openMenu === "fleet" && (
            <div className="nav-menu" onClick={(e) => e.stopPropagation()}>
              {window.FLEET.map(c => (
                <a key={c.id} className="nav-menu-item" onClick={(e) => pickCar(c, e)}>
                  <span className="nav-menu-dot" style={{background: navDotColor(c.id)}}></span>
                  <span style={{flex:1}}>
                    <span style={{display:"block", fontWeight:600, fontSize:14, color:"var(--ink-900)"}}>{c.name}</span>
                    <span style={{display:"block", fontSize:12, color:"var(--ink-500)", marginTop:2}}>{c.cat} · XCD {c.pricePerDay}/day</span>
                  </span>
                  <Icon name="arrow-right" size={14} color="var(--ink-400)" />
                </a>
              ))}
            </div>
          )}
        </div>
        <div className={`nav-dropdown ${openMenu === "locations" ? "is-open" : ""}`}>
          <a className="nav-dropdown-trigger" onClick={(e) => toggle("locations", e)}>
            Locations <Icon name="chevron-down" size={12} />
          </a>
          {openMenu === "locations" && (
            <div className="nav-menu nav-menu--locations" onClick={(e) => e.stopPropagation()}>
              <div className="nav-menu-section-label">Branches</div>
              {window.LOCATIONS.filter(l => ["kingstown", "aia", "ferry"].includes(l.id)).map(l => (
                <a key={l.id} className="nav-menu-item" onClick={(e) => { e.stopPropagation(); setOpenMenu(null); }}>
                  <span className="nav-menu-dot" style={{background: "#f5b800"}}></span>
                  <span style={{flex:1}}>
                    <span style={{display:"block", fontWeight:600, fontSize:14, color:"var(--ink-900)"}}>{l.name}</span>
                    <span style={{display:"block", fontSize:12, color:"var(--ink-500)", marginTop:2}}>{l.detail}</span>
                  </span>
                  <Icon name="pin" size={14} color="var(--ink-400)" />
                </a>
              ))}
              <div className="nav-menu-section-label" style={{marginTop:8}}>Hotel &amp; villa pickup</div>
              {window.LOCATIONS.filter(l => !["kingstown", "aia", "ferry"].includes(l.id)).map(l => (
                <a key={l.id} className="nav-menu-item" onClick={(e) => { e.stopPropagation(); setOpenMenu(null); }}>
                  <span className="nav-menu-dot" style={{background: "var(--sea-500)"}}></span>
                  <span style={{flex:1}}>
                    <span style={{display:"block", fontWeight:600, fontSize:14, color:"var(--ink-900)"}}>{l.name}</span>
                    <span style={{display:"block", fontSize:12, color:"var(--ink-500)", marginTop:2}}>{l.detail}</span>
                  </span>
                  <Icon name="pin" size={14} color="var(--ink-400)" />
                </a>
              ))}
            </div>
          )}
        </div>
        <a>Long-term</a>
        <a>Contact us</a>
        <div className="nav-social">
          <a className="nav-social-btn" aria-label="WhatsApp" href="https://wa.me/17844584001" target="_blank" rel="noreferrer"><Icon name="whatsapp" size={16} /></a>
          <a className="nav-social-btn" aria-label="Instagram" href="https://instagram.com" target="_blank" rel="noreferrer"><Icon name="instagram" size={16} /></a>
          <a className="nav-social-btn" aria-label="Facebook" href="https://facebook.com" target="_blank" rel="noreferrer"><Icon name="facebook" size={16} /></a>
        </div>
        <a className="nav-phone" href="tel:+17844584001">
          <Icon name="phone" size={14} />
          <span>+1 784 458 4001</span>
        </a>
      </div>
    </nav>
  );
};

const navDotColor = (id) => ({
  "toyota-noah": "#ff6b4a",
  "suzuki-sx4-2012": "#f5b800",
  "toyota-vitz-2017": "#0fa098",
  "suzuki-escudo-2016": "#ff6b4a",
  "toyota-corolla-axio-2017": "#15303f",
  "suzuki-vitara-2017": "#0a6764",
}[id] || "#0fa098");

// Hero scenery — Kingstown photo
const HeroScene = () => (
  <div style={{position: "absolute", inset: 0, width: "100%", height: "100%"}}>
    <img src="assets/kingstown-hero.webp" alt="Kingstown, St. Vincent" style={{width: "100%", height: "100%", objectFit: "cover", display: "block"}} />
  </div>
);

// Destination card SVG (small landscape)
const DestSVG = ({ variant }) => {
  const palettes = {
    a: { sky1: "#ffd189", sky2: "#5dc4d4", sea: "#0fa098", mtn: "#0c524f", sand: "#f5ecda" },
    b: { sky1: "#ffb27a", sky2: "#1ab2aa", sea: "#0a807a", mtn: "#0a3d3b", sand: "#ebdcb8" },
    c: { sky1: "#ffe177", sky2: "#7fdbd1", sea: "#2bbab2", mtn: "#13524d", sand: "#fbf6ee" },
    d: { sky1: "#ff8a6e", sky2: "#5dc4d4", sea: "#0fa098", mtn: "#0a6764", sand: "#f5ecda" },
  }[variant];
  return (
    <svg viewBox="0 0 800 400" preserveAspectRatio="xMidYMid slice" className="dest-svg">
      <defs>
        <linearGradient id={`dsky-${variant}`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor={palettes.sky1} />
          <stop offset="100%" stopColor={palettes.sky2} />
        </linearGradient>
      </defs>
      <rect width="800" height="400" fill={`url(#dsky-${variant})`} />
      <circle cx="600" cy="120" r="38" fill="#fff5c2" opacity="0.85" />
      <path d="M0 240 L120 180 L260 220 L380 160 L520 210 L660 170 L800 220 L800 280 L0 280 Z" fill={palettes.mtn} opacity="0.7" />
      <rect y="260" width="800" height="80" fill={palettes.sea} />
      <path d="M0 340 Q200 335 400 342 T800 340 L800 400 L0 400 Z" fill={palettes.sand} />
      {/* palm */}
      <g transform="translate(80 340)">
        <path d="M0 0 Q-2 -50 4 -110" stroke="#1f140d" strokeWidth="5" fill="none" strokeLinecap="round" />
        <g transform="translate(4 -110)">
          <path d="M0 0 Q-25 -5 -55 6 Q-35 -12 -60 -18" stroke="#0c524f" strokeWidth="5" fill="none" />
          <path d="M0 0 Q25 -5 55 6 Q35 -12 60 -18" stroke="#0c524f" strokeWidth="5" fill="none" />
          <path d="M0 0 Q-12 -25 -28 -50" stroke="#0a3d3b" strokeWidth="5" fill="none" />
          <path d="M0 0 Q12 -25 28 -50" stroke="#0a3d3b" strokeWidth="5" fill="none" />
        </g>
      </g>
    </svg>
  );
};

// ===== CAR ILLUSTRATIONS =====

const CarSedan = ({ color = "#0fa098" }) => (
  <svg viewBox="0 0 320 140" preserveAspectRatio="xMidYMid meet" style={{width:"100%", height:"100%", display:"block"}} xmlns="http://www.w3.org/2000/svg">
    {/* shadow */}
    <ellipse cx="160" cy="125" rx="120" ry="6" fill="#000" opacity="0.12" />
    {/* body */}
    <path d="M30 95 L30 80 Q30 70 40 68 L75 60 Q90 38 110 32 Q140 26 175 26 Q210 26 240 36 L275 58 L295 64 Q300 66 300 72 L300 95 Z" fill={color} />
    {/* roof highlight */}
    <path d="M85 60 Q100 42 125 36 Q160 30 195 32 Q220 34 245 44 L235 56 Q210 48 180 46 Q145 44 115 50 Q100 54 90 62 Z" fill="#fff" opacity="0.18" />
    {/* windows */}
    <path d="M95 60 Q110 45 130 40 Q155 36 175 38 L170 60 Z" fill="#1c2934" opacity="0.85" />
    <path d="M180 38 Q205 40 228 50 L235 60 L180 60 Z" fill="#1c2934" opacity="0.85" />
    {/* door line */}
    <path d="M165 38 L165 95" stroke="#000" strokeWidth="0.6" opacity="0.2" />
    {/* lights */}
    <rect x="290" y="72" width="10" height="6" rx="1" fill="#ffd23f" />
    <rect x="32" y="78" width="10" height="6" rx="1" fill="#fff" opacity="0.7" />
    {/* bottom shadow */}
    <rect x="30" y="92" width="270" height="3" fill="#000" opacity="0.2" />
    {/* wheels */}
    <circle cx="85" cy="98" r="20" fill="#1c2025" />
    <circle cx="85" cy="98" r="11" fill="#3a4047" />
    <circle cx="85" cy="98" r="4" fill="#1c2025" />
    <circle cx="245" cy="98" r="20" fill="#1c2025" />
    <circle cx="245" cy="98" r="11" fill="#3a4047" />
    <circle cx="245" cy="98" r="4" fill="#1c2025" />
  </svg>
);

const CarSUV = ({ color = "#15303f" }) => (
  <svg viewBox="0 0 320 140" preserveAspectRatio="xMidYMid meet" style={{width:"100%", height:"100%", display:"block"}} xmlns="http://www.w3.org/2000/svg">
    <ellipse cx="160" cy="125" rx="124" ry="6" fill="#000" opacity="0.12" />
    {/* body */}
    <path d="M28 95 L28 76 Q28 65 38 62 L62 56 Q70 32 92 28 Q130 22 175 22 Q220 22 250 32 L268 50 L292 56 Q300 58 300 66 L300 95 Z" fill={color} />
    {/* roof rack */}
    <rect x="78" y="20" width="140" height="3" rx="1" fill="#1c2025" />
    <rect x="78" y="20" width="3" height="8" fill="#1c2025" />
    <rect x="215" y="20" width="3" height="8" fill="#1c2025" />
    {/* highlight */}
    <path d="M70 56 Q80 32 105 28 Q150 22 195 24 Q230 28 255 38 L246 52 Q220 44 180 42 Q140 40 110 46 Q90 50 80 60 Z" fill="#fff" opacity="0.14" />
    {/* windows */}
    <path d="M78 56 Q90 36 115 32 Q150 28 175 30 L172 56 Z" fill="#1c2934" opacity="0.85" />
    <path d="M180 30 Q210 32 240 44 L250 56 L180 56 Z" fill="#1c2934" opacity="0.85" />
    {/* door line */}
    <path d="M168 30 L168 95" stroke="#000" strokeWidth="0.6" opacity="0.2" />
    {/* fenders */}
    <path d="M58 95 Q58 70 88 70 Q118 70 118 95 Z" fill="#000" opacity="0.18" />
    <path d="M218 95 Q218 70 248 70 Q278 70 278 95 Z" fill="#000" opacity="0.18" />
    {/* lights */}
    <rect x="290" y="68" width="10" height="6" rx="1" fill="#ffd23f" />
    <rect x="30" y="74" width="10" height="6" rx="1" fill="#fff" opacity="0.7" />
    {/* bottom guard */}
    <rect x="28" y="92" width="272" height="3" fill="#000" opacity="0.25" />
    {/* wheels — bigger for SUV */}
    <circle cx="88" cy="98" r="22" fill="#1c2025" />
    <circle cx="88" cy="98" r="13" fill="#3a4047" />
    <circle cx="88" cy="98" r="5" fill="#1c2025" />
    <circle cx="248" cy="98" r="22" fill="#1c2025" />
    <circle cx="248" cy="98" r="13" fill="#3a4047" />
    <circle cx="248" cy="98" r="5" fill="#1c2025" />
  </svg>
);

const CarVan = ({ color = "#ff6b4a" }) => (
  <svg viewBox="0 0 320 140" preserveAspectRatio="xMidYMid meet" style={{width:"100%", height:"100%", display:"block"}} xmlns="http://www.w3.org/2000/svg">
    <ellipse cx="160" cy="125" rx="128" ry="6" fill="#000" opacity="0.12" />
    {/* body — boxier, taller */}
    <path d="M22 95 L22 70 Q22 58 32 56 L48 26 Q52 22 60 22 L260 22 Q270 22 274 30 L290 50 L298 54 Q304 56 304 64 L304 95 Z" fill={color} />
    {/* highlight */}
    <path d="M50 28 L260 28 L268 50 L48 50 Z" fill="#fff" opacity="0.12" />
    {/* windows */}
    <path d="M58 30 Q56 32 56 36 L56 50 L120 50 L120 30 Z" fill="#1c2934" opacity="0.85" />
    <path d="M128 30 L188 30 L188 50 L128 50 Z" fill="#1c2934" opacity="0.85" />
    <path d="M196 30 L256 30 Q260 30 264 36 L268 50 L196 50 Z" fill="#1c2934" opacity="0.85" />
    {/* sliding door line */}
    <path d="M188 50 L188 95" stroke="#000" strokeWidth="0.6" opacity="0.25" />
    <path d="M124 50 L124 95" stroke="#000" strokeWidth="0.6" opacity="0.25" />
    {/* handle */}
    <rect x="145" y="62" width="14" height="3" rx="1" fill="#000" opacity="0.25" />
    {/* lights */}
    <rect x="294" y="64" width="10" height="6" rx="1" fill="#ffd23f" />
    <rect x="24" y="68" width="10" height="6" rx="1" fill="#fff" opacity="0.7" />
    {/* bottom guard */}
    <rect x="22" y="92" width="282" height="3" fill="#000" opacity="0.22" />
    {/* wheels */}
    <circle cx="80" cy="98" r="20" fill="#1c2025" />
    <circle cx="80" cy="98" r="11" fill="#3a4047" />
    <circle cx="80" cy="98" r="4" fill="#1c2025" />
    <circle cx="250" cy="98" r="20" fill="#1c2025" />
    <circle cx="250" cy="98" r="11" fill="#3a4047" />
    <circle cx="250" cy="98" r="4" fill="#1c2025" />
  </svg>
);

const CarJimny = ({ color = "#f5b800" }) => (
  <svg viewBox="0 0 320 140" preserveAspectRatio="xMidYMid meet" style={{width:"100%", height:"100%", display:"block"}} xmlns="http://www.w3.org/2000/svg">
    <ellipse cx="160" cy="125" rx="118" ry="6" fill="#000" opacity="0.12" />
    {/* body — boxy */}
    <path d="M40 95 L40 70 L52 60 L52 30 Q52 24 60 24 L240 24 Q248 24 248 30 L248 60 L268 60 Q280 62 286 70 L286 95 Z" fill={color} />
    {/* roof rack */}
    <rect x="60" y="18" width="180" height="3" rx="1" fill="#1c2025" />
    <rect x="60" y="18" width="3" height="6" fill="#1c2025" />
    <rect x="237" y="18" width="3" height="6" fill="#1c2025" />
    {/* highlight */}
    <path d="M52 30 L248 30 L248 38 L52 38 Z" fill="#fff" opacity="0.18" />
    {/* windows */}
    <path d="M58 32 L138 32 L138 58 L58 58 Z" fill="#1c2934" opacity="0.85" />
    <path d="M146 32 L242 32 L242 58 L146 58 Z" fill="#1c2934" opacity="0.85" />
    {/* spare tire on back */}
    <circle cx="285" cy="78" r="10" fill="#1c2025" />
    <circle cx="285" cy="78" r="5" fill="#3a4047" />
    {/* fenders */}
    <path d="M65 95 Q65 72 92 72 Q119 72 119 95 Z" fill="#000" opacity="0.22" />
    <path d="M205 95 Q205 72 232 72 Q259 72 259 95 Z" fill="#000" opacity="0.22" />
    {/* lights */}
    <rect x="270" y="64" width="14" height="6" rx="1" fill="#ffd23f" />
    <circle cx="48" cy="72" r="4" fill="#fff" opacity="0.8" />
    {/* bottom guard */}
    <rect x="40" y="92" width="246" height="3" fill="#000" opacity="0.25" />
    {/* wheels — chunky */}
    <circle cx="92" cy="100" r="22" fill="#1c2025" />
    <circle cx="92" cy="100" r="14" fill="#3a4047" />
    <circle cx="92" cy="100" r="5" fill="#1c2025" />
    <circle cx="232" cy="100" r="22" fill="#1c2025" />
    <circle cx="232" cy="100" r="14" fill="#3a4047" />
    <circle cx="232" cy="100" r="5" fill="#1c2025" />
  </svg>
);

const CarIllustration = ({ id }) => {
  switch (id) {
    case "toyota-vitz-2017": return <CarSedan color="#0fa098" />;
    case "toyota-corolla-axio-2017": return <CarSedan color="#15303f" />;
    case "suzuki-sx4-2012": return <CarSUV color="#f5b800" />;
    case "suzuki-vitara-2017": return <CarSUV color="#0a6764" />;
    case "suzuki-escudo-2016": return <CarJimny color="#ff6b4a" />;
    case "toyota-noah": return <CarVan color="#ff6b4a" />;
    default: return <CarSedan />;
  }
};

const Footer = () => (
  <footer className="footer">
    <div className="footer-grid">
      <div className="footer-brand">
        <a className="brand">
          <img src="assets/784-logo.webp" alt="784 Motors" className="brand-logo" />
        </a>
        <p>Family-owned car rental in St. Vincent and the Grenadines since 2009. Three branches, honest pricing, sweet rides.</p>
      </div>
      <div className="footer-col">
        <h4>Locations</h4>
        <a>Kingstown · Bay Street</a>
        <a>Arnos Vale · Plaza</a>
        <a>AIA Argyle Airport</a>
        <a>Long-term rentals</a>
      </div>
      <div className="footer-col">
        <h4>Help</h4>
        <a>Driver's permit info</a>
        <a>Insurance & cover</a>
        <a>Cancellation policy</a>
        <a>Contact us</a>
      </div>
      <div className="footer-col">
        <h4>Reach us</h4>
        <a><Icon name="phone" size={14} />&nbsp;&nbsp;+1 784 458 4001</a>
        <a><Icon name="whatsapp" size={14} />&nbsp;&nbsp;WhatsApp 24/7</a>
        <a>hello@784motors.vc</a>
      </div>
    </div>
    <div className="footer-bottom">
      <div>© 2026 784 Motors Ltd. · St. Vincent & the Grenadines</div>
      <div className="footer-socials">
        <a><Icon name="instagram" size={16} /></a>
        <a><Icon name="facebook" size={16} /></a>
        <a><Icon name="whatsapp" size={16} /></a>
      </div>
    </div>
  </footer>
);

Object.assign(window, { Nav, HeroScene, DestSVG, CarIllustration, CarSedan, CarSUV, CarVan, CarJimny, Footer });
