const NAVBAR_BOOKING_URL = 'https://www.fresha.com/a/hayre-grooming-salon-abu-dhabi-hayre-grooming-salon-united-square-gwywnytl/booking';
const NAVBAR_WHATSAPP_URL = 'https://wa.me/971563690151?text=Hi%20HAYRE%2C%20I%27d%20like%20to%20book%20an%20appointment.';
const NAVBAR_CALL_URL = 'tel:+971563690151';

const iconBaseProps = {
  fill: 'none',
  stroke: 'currentColor',
  strokeLinecap: 'round',
  strokeLinejoin: 'round',
  vectorEffect: 'non-scaling-stroke',
  'aria-hidden': 'true',
};

const PhoneIcon = ({ className }) => (
  <svg viewBox="0 0 24 24" className={className} {...iconBaseProps} strokeWidth="1.8">
    <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.86 19.86 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6A19.86 19.86 0 0 1 2.12 4.18 2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.12.9.34 1.78.65 2.62a2 2 0 0 1-.45 2.11L8.04 9.72a16 16 0 0 0 6.24 6.24l1.27-1.27a2 2 0 0 1 2.11-.45c.84.31 1.72.53 2.62.65A2 2 0 0 1 22 16.92Z" />
  </svg>
);

const WhatsAppIcon = ({ className }) => (
  <svg viewBox="0 0 24 24" className={className} aria-hidden="true">
    <path
      fill="currentColor"
      d="M12.01 2.25A9.74 9.74 0 0 0 3.6 16.9L2.25 21.75l4.96-1.3a9.74 9.74 0 1 0 4.8-18.2Zm0 1.82a7.92 7.92 0 0 1 7.92 7.92 7.92 7.92 0 0 1-12 6.81l-.4-.23-2.87.75.77-2.8-.24-.39a7.92 7.92 0 0 1 6.82-12.06Zm-2.22 3.6c-.2 0-.52.08-.8.39-.28.31-1.06 1.04-1.06 2.54 0 1.5 1.09 2.95 1.24 3.15.14.19 2.15 3.29 5.21 4.48 2.54.98 3.07.79 3.62.74.56-.05 1.8-.74 2.05-1.46.25-.72.25-1.33.17-1.46-.08-.12-.29-.19-.6-.34-.31-.14-1.86-.91-2.15-1.01-.29-.1-.5-.15-.71.16-.21.31-.81 1-1 1.21-.18.22-.37.24-.68.08-.31-.15-1.31-.48-2.49-1.54-.92-.82-1.54-1.83-1.72-2.14-.18-.31-.02-.48.14-.63.14-.14.31-.35.47-.52.16-.17.21-.29.31-.49.1-.2.05-.38-.02-.53-.08-.15-.71-1.72-.97-2.35-.23-.58-.48-.59-.71-.6h-.6Z"
    />
  </svg>
);

const CalendarIcon = ({ className }) => (
  <svg viewBox="0 0 24 24" className={className} {...iconBaseProps} strokeWidth="1.8">
    <path d="M7 2.75v3.5M17 2.75v3.5M3.75 8.25h16.5" />
    <rect x="3.75" y="4.75" width="16.5" height="15.5" rx="2.75" />
  </svg>
);

const InstagramIcon = ({ className }) => (
  <svg viewBox="0 0 24 24" className={className} {...iconBaseProps} strokeWidth="1.75">
    <rect x="3" y="3" width="18" height="18" rx="5.25" />
    <circle cx="12" cy="12" r="4.1" />
    <circle cx="17.2" cy="6.8" r="0.9" fill="currentColor" stroke="none" />
  </svg>
);

const FreshaIcon = ({ className }) => (
  <svg viewBox="0 0 24 24" className={className} {...iconBaseProps} strokeWidth="1.75">
    <path d="M6 3h12l2.25 4H3.75L6 3Z" />
    <rect x="4" y="7.25" width="16" height="13" rx="2.2" />
    <path d="M8 11.5h8M8 15.5h5" />
  </svg>
);

const ArrowIcon = ({ className }) => (
  <svg viewBox="0 0 24 24" className={className} {...iconBaseProps} strokeWidth="1.7">
    <path d="M5 12h14" />
    <path d="m13 6 6 6-6 6" />
  </svg>
);

window.HayreIcons = { PhoneIcon, WhatsAppIcon, CalendarIcon, InstagramIcon, FreshaIcon, ArrowIcon };

const Navbar = ({ active = 'home', onNav }) => {
  const [scrolled, setScrolled] = React.useState(false);
  const [open, setOpen] = React.useState(false);
  const headerRef = React.useRef(null);

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

  // Close the mobile menu on outside click or Escape
  React.useEffect(() => {
    if (!open) return;
    const onDown = (e) => {
      if (headerRef.current && !headerRef.current.contains(e.target)) setOpen(false);
    };
    const onKey = (e) => { if (e.key === 'Escape') setOpen(false); };
    document.addEventListener('pointerdown', onDown);
    document.addEventListener('keydown', onKey);
    return () => {
      document.removeEventListener('pointerdown', onDown);
      document.removeEventListener('keydown', onKey);
    };
  }, [open]);

  const items = [
    { id: 'home', label: 'Home' },
    { id: 'studio', label: 'Studio' },
    { id: 'services', label: 'Services' },
    { id: 'about', label: 'About' },
    { id: 'contact', label: 'Contact' },
  ];

  const handle = (id) => {
    setOpen(false);
    onNav && onNav(id);
  };

  return (
    <header ref={headerRef} className={'navbar' + (scrolled ? ' scrolled' : '') + (open ? ' menu-open' : '')}>
      <nav className="navbar-links">
        {items.map((it) => (
          <button
            key={it.id}
            className={'navbar-link' + (active === it.id ? ' active' : '')}
            onClick={() => handle(it.id)}
            data-analytics-event="navigation_click"
            data-analytics-content-type="navigation"
            data-analytics-item-id={it.id}
            data-analytics-label={it.label}
          >
            {it.label}
          </button>
        ))}
      </nav>

      <a href="#home" className="navbar-brand" onClick={(e) => { e.preventDefault(); handle('home'); }}>
        <img src="assets/HAYRE-logo-white.webp" alt="HAYRE" className="navbar-logo navbar-logo-placeholder" aria-hidden="true" />
      </a>

      <div className="navbar-right">
        <div className="navbar-actions" aria-label="Quick actions">
          <a
            href={NAVBAR_CALL_URL}
            className="navbar-action"
            aria-label="Call HAYRE"
          >
            <PhoneIcon />
          </a>
          <a
            href={NAVBAR_WHATSAPP_URL}
            target="_blank"
            rel="noopener noreferrer"
            className="navbar-action navbar-action-whatsapp"
            aria-label="Chat on WhatsApp"
          >
            <WhatsAppIcon />
          </a>
          <a
            href={NAVBAR_BOOKING_URL}
            target="_blank"
            rel="noopener noreferrer"
            className="navbar-action"
            aria-label="Book appointment"
          >
            <CalendarIcon />
          </a>
        </div>
        <button className={'navbar-burger' + (open ? ' open' : '')} onClick={() => setOpen(!open)} aria-label="Menu">
          <span></span><span></span>
        </button>
      </div>

      <div className={'navbar-mobile' + (open ? ' open' : '')}>
        {items.map((it, i) => (
          <button
            key={it.id}
            className={'navbar-mobile-link' + (active === it.id ? ' active' : '')}
            onClick={() => handle(it.id)}
            data-analytics-event="navigation_click"
            data-analytics-content-type="navigation"
            data-analytics-item-id={it.id}
            data-analytics-label={it.label}
          >
            <span className="num">0{i + 1}</span>
            <span>{it.label}</span>
          </button>
        ))}
        <a href={NAVBAR_BOOKING_URL} target="_blank" rel="noopener noreferrer" className="navbar-mobile-book">
          Book Appointment
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
        </a>
      </div>
    </header>
  );
};

window.Navbar = Navbar;
