// Shared components for Next Moon website

const { useState, useEffect, useRef } = React;

/* ============================================
   ICONS
   ============================================ */
const Icon = {
  Arrow: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" className="arrow">
      <path d="M5 12h14M13 5l7 7-7 7" />
    </svg>
  ),
  ArrowLeft: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round">
      <path d="M19 12H5M11 5l-7 7 7 7" />
    </svg>
  ),
  Phone: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/>
    </svg>
  ),
  Mail: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/>
      <polyline points="22,6 12,13 2,6"/>
    </svg>
  ),
  Location: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"/>
      <circle cx="12" cy="10" r="3"/>
    </svg>
  ),
  Whatsapp: () => (
    <svg width="28" height="28" viewBox="0 0 32 32" fill="currentColor">
      <path d="M16 3C9.373 3 4 8.373 4 15c0 2.338.673 4.518 1.83 6.362L4 29l7.86-1.792A11.95 11.95 0 0 0 16 27c6.627 0 12-5.373 12-12S22.627 3 16 3zm0 22c-1.78 0-3.434-.47-4.87-1.29l-.34-.2-4.66 1.06 1.07-4.54-.22-.36A9.95 9.95 0 0 1 6 15c0-5.514 4.486-10 10-10s10 4.486 10 10-4.486 10-10 10zm5.2-7.4c-.3-.15-1.8-.89-2.08-.99-.28-.1-.48-.15-.69.15-.2.3-.79.99-.97 1.2-.18.2-.36.23-.66.08-.3-.15-1.26-.46-2.4-1.48-.89-.79-1.49-1.77-1.66-2.07-.17-.3-.02-.46.13-.61.13-.13.3-.36.45-.54.15-.17.2-.3.3-.5.1-.2.05-.38-.02-.53-.08-.15-.69-1.65-.94-2.26-.25-.6-.5-.52-.69-.53-.18-.01-.38-.01-.58-.01s-.53.08-.81.38c-.28.3-1.07 1.05-1.07 2.55s1.1 2.96 1.25 3.16c.15.2 2.17 3.31 5.26 4.64.74.32 1.31.51 1.76.65.74.24 1.41.2 1.94.12.59-.09 1.8-.74 2.06-1.45.25-.71.25-1.32.18-1.45-.07-.13-.27-.2-.57-.35z"/>
    </svg>
  ),
  Instagram: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <rect x="2" y="2" width="20" height="20" rx="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>
  ),
  Facebook: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <path d="M18 2h-3a5 5 0 0 0-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 0 1 1-1h3z"/>
    </svg>
  ),
  Tiktok: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
      <path d="M19.59 6.69a4.83 4.83 0 0 1-3.77-4.25V2h-3.45v13.67a2.89 2.89 0 0 1-5.2 1.74 2.89 2.89 0 0 1 2.31-4.64 2.93 2.93 0 0 1 .88.13V9.4a6.84 6.84 0 0 0-1-.05A6.33 6.33 0 0 0 5.8 20.1a6.34 6.34 0 0 0 10.86-4.43v-7a8.16 8.16 0 0 0 4.77 1.52v-3.4a4.85 4.85 0 0 1-1.84-.1z"/>
    </svg>
  ),
  Menu: () => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <line x1="3" y1="6" x2="21" y2="6"/>
      <line x1="3" y1="12" x2="21" y2="12"/>
      <line x1="3" y1="18" x2="21" y2="18"/>
    </svg>
  ),
  Close: () => (
    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5">
      <line x1="18" y1="6" x2="6" y2="18"/>
      <line x1="6" y1="6" x2="18" y2="18"/>
    </svg>
  ),
};

/* Service icons */
const ServiceIcon = {
  Home: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M3 11l9-8 9 8"/>
      <path d="M5 10v10h14V10"/>
      <path d="M10 20v-6h4v6"/>
    </svg>
  ),
  Bath: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M4 12h16v4a3 3 0 0 1-3 3H7a3 3 0 0 1-3-3v-4z"/>
      <path d="M7 12V6a2 2 0 0 1 4 0"/>
      <line x1="9" y1="7" x2="11" y2="7"/>
      <line x1="6" y1="22" x2="7" y2="20"/>
      <line x1="17" y1="22" x2="18" y2="20"/>
    </svg>
  ),
  Kitchen: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <rect x="4" y="3" width="16" height="18" rx="1"/>
      <line x1="4" y1="9" x2="20" y2="9"/>
      <line x1="4" y1="15" x2="20" y2="15"/>
      <circle cx="8" cy="6" r="0.8" fill="currentColor"/>
      <circle cx="12" cy="6" r="0.8" fill="currentColor"/>
    </svg>
  ),
  Paint: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 2l3 5h-6l3-5z"/>
      <rect x="4" y="7" width="16" height="5" rx="1"/>
      <path d="M12 12v4"/>
      <path d="M9 16h6v6H9z"/>
    </svg>
  ),
  Building: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <rect x="4" y="3" width="16" height="18"/>
      <line x1="8" y1="7" x2="8" y2="7.01"/>
      <line x1="12" y1="7" x2="12" y2="7.01"/>
      <line x1="16" y1="7" x2="16" y2="7.01"/>
      <line x1="8" y1="11" x2="8" y2="11.01"/>
      <line x1="12" y1="11" x2="12" y2="11.01"/>
      <line x1="16" y1="11" x2="16" y2="11.01"/>
      <line x1="8" y1="15" x2="8" y2="15.01"/>
      <line x1="16" y1="15" x2="16" y2="15.01"/>
      <path d="M10 21v-3h4v3"/>
    </svg>
  ),
  Tools: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"/>
    </svg>
  ),
  Roof: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M2 12L12 4l10 8"/>
      <path d="M5 10v10h14V10"/>
      <line x1="9" y1="14" x2="15" y2="14"/>
      <line x1="9" y1="17" x2="15" y2="17"/>
    </svg>
  ),
  Water: () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 2s6 6 6 11a6 6 0 0 1-12 0c0-5 6-11 6-11z"/>
    </svg>
  ),
};

/* ============================================
   NAV
   ============================================ */
function Nav({ current, setPage }) {
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 20);
    onScroll();
    window.addEventListener('scroll', onScroll);
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const pages = [
    { id: 'home', label: 'Inicio' },
    { id: 'servicios', label: 'Servicios' },
    { id: 'comunidades', label: 'Comunidades' },
    { id: 'nosotros', label: 'Nosotros' },
    { id: 'contacto', label: 'Contacto' },
  ];

  const go = (id) => {
    setPage(id);
    setOpen(false);
    window.scrollTo({ top: 0, behavior: 'instant' });
  };

  return (
    <nav className={`nav ${scrolled ? 'scrolled' : ''}`}>
      <div className="nav-inner">
        <a className="nav-logo" onClick={() => go('home')}>
          <span className="mark" />
          <span className="wordmark">
            <span>NextMoon</span>
            <small>Reformas · SL</small>
          </span>
        </a>
        <div className={`nav-links ${open ? 'open' : ''}`}>
          {pages.map(p => (
            <a key={p.id}
               className={current === p.id ? 'active' : ''}
               onClick={() => go(p.id)}>
              {p.label}
            </a>
          ))}
        </div>
        <a className="nav-cta" onClick={() => go('contacto')}>
          Pedir presupuesto <Icon.Arrow />
        </a>
        <button className="nav-toggle" onClick={() => setOpen(!open)} aria-label="Menu">
          {open ? <Icon.Close /> : <Icon.Menu />}
        </button>
      </div>
    </nav>
  );
}

/* ============================================
   FOOTER
   ============================================ */
function Footer({ setPage }) {
  const go = (id) => {
    setPage(id);
    window.scrollTo({ top: 0, behavior: 'instant' });
  };
  return (
    <footer className="footer">
      <div className="container">
        <div className="footer-grid">
          <div className="footer-brand">
            <div className="nav-logo" style={{color: 'var(--cream)'}}>
              <span className="mark" />
              <span className="wordmark">
                <span>NextMoon</span>
                <small>Reformas · SL</small>
              </span>
            </div>
            <p>Reformas integrales, baños, cocinas y rehabilitación para comunidades en Terrassa y el Vallès Occidental. Calidad, confianza y acabados profesionales.</p>
            <div className="footer-socials">
              <a href="#" aria-label="Instagram"><Icon.Instagram /></a>
              <a href="#" aria-label="Facebook"><Icon.Facebook /></a>
              <a href="#" aria-label="TikTok"><Icon.Tiktok /></a>
            </div>
          </div>
          <div className="footer-col">
            <h5>Empresa</h5>
            <ul>
              <li><a onClick={() => go('nosotros')}>Sobre nosotros</a></li>
              <li><a onClick={() => go('servicios')}>Servicios</a></li>
              <li><a onClick={() => go('contacto')}>Contacto</a></li>
            </ul>
          </div>
          <div className="footer-col">
            <h5>Servicios</h5>
            <ul>
              <li><a onClick={() => go('servicios')}>Reformas integrales</a></li>
              <li><a onClick={() => go('servicios')}>Baños y cocinas</a></li>
              <li><a onClick={() => go('servicios')}>Pintura</a></li>
              <li><a onClick={() => go('comunidades')}>Comunidades</a></li>
            </ul>
          </div>
          <div className="footer-col">
            <h5>Contacto</h5>
            <ul>
              <li><a href="tel:+34614007303">+34 614 007 303</a></li>
              <li><a href="mailto:nextmoon@gpamoon.com">nextmoon@gpamoon.com</a></li>
              <li style={{color:'rgba(250,247,242,0.5)', fontSize:'13px'}}>C/ Montsià 15<br/>08233 Vacarisses (Barcelona)</li>
            </ul>
          </div>
        </div>
        <div className="footer-bottom">
          <span>© 2026 Next Moon S.L. · CIF B09704040 · Todos los derechos reservados</span>
          <div className="legal-links">
            <a onClick={() => go('aviso-legal')}>Aviso legal</a>
            <a onClick={() => go('privacidad')}>Privacidad</a>
            <a onClick={() => go('cookies')}>Cookies</a>
          </div>
        </div>
      </div>
    </footer>
  );
}

/* ============================================
   WHATSAPP FLOAT
   ============================================ */
function WhatsappFloat() {
  return (
    <a className="whatsapp-float"
       href="https://wa.me/34614007303?text=Hola,%20quiero%20información%20sobre%20una%20reforma"
       target="_blank" rel="noopener">
      <span className="wa-label">¿Hablamos por WhatsApp?</span>
      <span className="wa-btn"><Icon.Whatsapp /></span>
    </a>
  );
}

/* ============================================
   PLACEHOLDER IMAGE
   ============================================ */
function PhImage({ label, num, tone = 'navy', className = '' }) {
  const bg = tone === 'navy'
    ? { background: 'linear-gradient(135deg, rgba(201,168,76,0.18), transparent 40%), repeating-linear-gradient(45deg, #2a3648 0px, #2a3648 2px, #1f2a3c 2px, #1f2a3c 16px)' }
    : { background: 'linear-gradient(135deg, rgba(201,168,76,0.12), transparent 40%), repeating-linear-gradient(45deg, var(--gray-100) 0px, var(--gray-100) 2px, var(--paper) 2px, var(--paper) 14px)' };
  const labelColor = tone === 'navy' ? 'rgba(250,247,242,0.6)' : 'var(--gray-500)';
  const numColor = tone === 'navy' ? 'var(--gold)' : 'var(--gold)';
  return (
    <div className={`project-placeholder ${className}`} style={bg}>
      {num && <div style={{ position: 'absolute', top: 20, left: 24, fontFamily: 'var(--f-display)', fontSize: 48, fontStyle: 'italic', color: numColor, lineHeight: 1 }}>{num}</div>}
      <div style={{ position: 'absolute', bottom: 20, left: 24, fontFamily: 'var(--f-mono)', fontSize: 10, color: labelColor, letterSpacing: '0.12em', textTransform: 'uppercase' }}>{label}</div>
    </div>
  );
}

Object.assign(window, {
  Icon, ServiceIcon, Nav, Footer, WhatsappFloat, PhImage,
  useState, useEffect, useRef
});
