// Shared UI components for Automatic Restaurant & Grill redesign.

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

// === ICONS (inline SVG, brand-neutral) ===
const Icon = {
  pin: (props) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" {...props}><path d="M12 22s7-7.5 7-13a7 7 0 1 0-14 0c0 5.5 7 13 7 13Z"/><circle cx="12" cy="9" r="2.5"/></svg>),
  phone: (props) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" {...props}><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-6A19.79 19.79 0 0 1 2.12 4.18 2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.12.89.34 1.76.66 2.59a2 2 0 0 1-.45 2.11L8 9.91a16 16 0 0 0 6 6l1.49-1.32a2 2 0 0 1 2.11-.45c.83.32 1.7.54 2.59.66A2 2 0 0 1 22 16.92Z"/></svg>),
  clock: (props) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" {...props}><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>),
  arrow: (props) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" {...props}><path d="M5 12h14M13 5l7 7-7 7"/></svg>),
  star: (props) => (<svg viewBox="0 0 24 24" fill="currentColor" {...props}><path d="M12 2l3.09 6.26L22 9.27l-5 4.87L18.18 22 12 18.27 5.82 22 7 14.14 2 9.27l6.91-1.01L12 2Z"/></svg>),
  external: (props) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" {...props}><path d="M15 3h6v6M10 14L21 3M21 14v5a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5"/></svg>),
  instagram: (props) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" {...props}><rect x="3" y="3" width="18" height="18" rx="5"/><circle cx="12" cy="12" r="4"/><circle cx="17.5" cy="6.5" r="1" fill="currentColor"/></svg>),
  facebook: (props) => (<svg viewBox="0 0 24 24" fill="currentColor" {...props}><path d="M13 22v-8h3l1-4h-4V7.5c0-1 .3-1.5 1.7-1.5H17V2.2C16.6 2.1 15.5 2 14.3 2 11.7 2 10 3.6 10 6.5V10H7v4h3v8h3Z"/></svg>),
  whatsapp: (props) => (<svg viewBox="0 0 24 24" fill="currentColor" {...props}><path d="M17.5 14.4c-.3-.1-1.7-.8-2-.9-.3-.1-.5-.1-.7.1-.2.3-.7.9-.9 1.1-.2.2-.3.2-.6.1-.3-.1-1.2-.4-2.3-1.4-.9-.8-1.5-1.7-1.6-2-.2-.3 0-.4.1-.6.1-.1.3-.3.4-.5.1-.2.2-.3.3-.5.1-.2 0-.4 0-.5-.1-.1-.7-1.6-.9-2.2-.2-.6-.5-.5-.7-.5h-.6c-.2 0-.5.1-.8.4-.3.3-1 1-1 2.5 0 1.5 1.1 2.9 1.2 3.1.2.2 2.1 3.2 5.1 4.5.7.3 1.2.5 1.7.6.7.2 1.3.2 1.8.1.5-.1 1.7-.7 1.9-1.4.2-.7.2-1.2.2-1.4-.1-.1-.3-.2-.6-.3ZM12 2C6.5 2 2 6.5 2 12c0 1.8.5 3.5 1.3 5L2 22l5.2-1.4c1.4.8 3.1 1.3 4.8 1.3 5.5 0 10-4.5 10-10S17.5 2 12 2Z"/></svg>),
  search: (props) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" {...props}><circle cx="11" cy="11" r="7"/><path d="M21 21l-4.3-4.3"/></svg>),
  check: (props) => (<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" {...props}><path d="M5 12l5 5 9-11"/></svg>),
  flame: (props) => (<svg viewBox="0 0 24 24" fill="currentColor" {...props}><path d="M12 2s2 3 2 6c0 1.5-1 2.5-2 2.5S10 9.5 10 8s-1-3-1-3-2 4-2 7c0 4.4 2.9 8 7 8s7-3.6 7-8c0-5-5-7-5-7-1 2-2 3-2 3-1-2-2-6-2-6Z"/></svg>),
};

// === REVIEW BADGE ===
function ReviewBadge({ compact = false }) {
  return (
    <a className="review-badge" href="https://talabat.com/uae/automatic-restaurant-grill" target="_blank" rel="noopener">
      <div className="review-badge__logo">t</div>
      <div className="review-badge__meta">
        <div style={{ display: 'flex', alignItems: 'baseline', gap: 6 }}>
          <span className="review-badge__num">4.7</span>
          <span className="review-badge__stars">★★★★★</span>
        </div>
        <div className="review-badge__caption">{compact ? '1,921 reviews · talabat' : '1,921 ratings on Talabat'}</div>
      </div>
    </a>
  );
}

// === NAVBAR ===
function Navbar({ route, navigate }) {
  const links = [
    { id: 'home', label: 'Home' },
    { id: 'menu', label: 'Menu' },
    { id: 'branches', label: 'Branches' },
    { id: 'catering', label: 'Catering' },
    { id: 'order', label: 'Order Online' },
    { id: 'about', label: 'Heritage' },
  ];
  return (
    <React.Fragment>
      <div className="topbar">
        <span>📍 12 outlets across Dubai &amp; Abu Dhabi — </span>
        <a onClick={() => navigate('branches')} style={{ cursor: 'pointer' }}>find your nearest branch →</a>
      </div>
      <nav className="nav">
        <div className="nav__inner">
          <div className="nav__brand" onClick={() => navigate('home')} style={{ cursor: 'pointer' }}>
            <img src="assets/logo-primary.png" alt="Automatic Restaurant" />
            <div>
              <div className="nav__wordmark">Automatic</div>
              <div className="nav__wordmark-sub">Restaurant &amp; Grill · 1977</div>
            </div>
          </div>
          <div className="nav__links">
            {links.map(l => (
              <div
                key={l.id}
                className={'nav__link ' + (route === l.id ? 'nav__link--active' : '')}
                onClick={() => navigate(l.id)}
              >
                {l.label}
              </div>
            ))}
          </div>
          <div className="nav__lang" title="Arabic launching Phase 2">
            <span className="nav__lang-active">EN</span>
            <span style={{ opacity: 0.4 }}>·</span>
            <span className="nav__lang-ghost">AR</span>
          </div>
          <div className="nav__cta">
            <button className="btn btn--ghost btn--sm" onClick={() => navigate('branches')}>Find branch</button>
            <button className="btn btn--primary btn--sm" onClick={() => navigate('order')}>Order now</button>
          </div>
        </div>
      </nav>
    </React.Fragment>
  );
}

// === FOOTER ===
function Footer({ navigate }) {
  return (
    <footer className="footer">
      <div className="container container--wide">
        <div className="footer__grid">
          <div className="footer__brand-block">
            <div className="footer__logo">
              <img src="assets/logo-primary.png" alt="Automatic" />
              <div className="footer__logo-wordmark">Automatic</div>
            </div>
            <p className="footer__about">
              Lebanese grill, mezze and shawarma — generously served across the UAE since 1977.
              Charcoal-fired, recipe-honest, family-portioned.
            </p>
            <div className="footer__social">
              <a href="https://instagram.com/automaticrestaurantuae" target="_blank" rel="noopener" aria-label="Instagram"><Icon.instagram width="16" height="16" /></a>
              <a href="https://facebook.com/automaticrestaurantuae" target="_blank" rel="noopener" aria-label="Facebook"><Icon.facebook width="16" height="16" /></a>
              <a href="https://wa.me/971565266357" target="_blank" rel="noopener" aria-label="WhatsApp"><Icon.whatsapp width="16" height="16" /></a>
            </div>
          </div>
          <div>
            <div className="footer__heading">Explore</div>
            <div className="footer__list">
              <a onClick={() => navigate('menu')}>Full menu</a>
              <a onClick={() => navigate('branches')}>Branch finder</a>
              <a onClick={() => navigate('catering')}>Catering</a>
              <a onClick={() => navigate('order')}>Order online</a>
              <a onClick={() => navigate('about')}>Our story</a>
            </div>
          </div>
          <div>
            <div className="footer__heading">Dubai</div>
            <div className="footer__list">
              <a onClick={() => navigate('branches')}>Al Raffa / Bur Dubai</a>
              <a onClick={() => navigate('branches')}>Jumeirah 1</a>
              <a onClick={() => navigate('branches')}>Mirdif</a>
              <a onClick={() => navigate('branches')}>Ibn Battuta Mall</a>
              <a onClick={() => navigate('branches')}>+ 5 more</a>
            </div>
          </div>
          <div>
            <div className="footer__heading">Abu Dhabi</div>
            <div className="footer__list">
              <a onClick={() => navigate('branches')}>Yas Mall</a>
              <a onClick={() => navigate('branches')}>Corniche · Khalidiya</a>
              <a onClick={() => navigate('branches')}>Al Manhal</a>
            </div>
            <div className="footer__heading" style={{ marginTop: 28 }}>Talk to us</div>
            <div className="footer__list">
              <a href="tel:+97143233002">HQ · 04 323 3002</a>
              <a href="tel:600546667">Call centre · 600 546 667</a>
              <a href="mailto:info@autorest.ae">info@autorest.ae</a>
            </div>
          </div>
        </div>
        <div className="footer__bottom">
          <div>© 1977 – {new Date().getFullYear()} Automatic Restaurant &amp; Grill · A DAMAC Properties brand</div>
          <div>Designed in Dubai · Served generously</div>
        </div>
      </div>
    </footer>
  );
}

// === DISH CARD ===
function DishCard({ dish }) {
  const tagLabel = { v: 'V', vg: 'VG', gf: 'GF', n: 'Nuts', best: '★ Best-seller' };
  const imgSrc = dish.image ? `assets/${dish.image}.jpg` : null;
  return (
    <div className="dish-card">
      {imgSrc
        ? <img className="dish-card__img" src={imgSrc} alt={dish.name} />
        : <div className="dish-card__img" style={{ background: 'repeating-linear-gradient(135deg, #EFE2CE 0 8px, #F5EBDC 8px 16px)' }} />
      }
      <div className="dish-card__body">
        <div className="dish-card__head">
          <span className="dish-card__name">{dish.name}</span>
          <span className="dish-card__name-ar">{dish.nameAr}</span>
        </div>
        <p className="dish-card__desc">{dish.desc}</p>
        <div className="dish-card__meta">
          <span className="dish-card__price"><span className="dish-card__price-currency">AED</span>{dish.price}</span>
          {dish.tags.map(t => (
            <span key={t} className={'tag tag--' + t}>{tagLabel[t]}</span>
          ))}
        </div>
      </div>
    </div>
  );
}

// === BRANCH CARD ===
function BranchCard({ branch, selected, onSelect }) {
  const dl = branch.deeplinks;
  const items = [
    { key: 'talabat', icon: '🍔', label: 'Talabat', url: dl.talabat },
    { key: 'deliveroo', icon: '🦘', label: 'Deliveroo', url: dl.deliveroo },
    { key: 'careem', icon: '🚗', label: 'Careem', url: dl.careem },
    { key: 'whatsapp', icon: '💬', label: 'WhatsApp', url: dl.whatsapp },
  ];
  return (
    <div className={'branch-card ' + (selected ? 'branch-card--selected' : '')} onClick={() => onSelect && onSelect(branch.id)}>
      <div className="branch-card__head">
        <div>
          <div className="branch-card__title">{branch.name}</div>
          <div className="branch-card__sub">{branch.sub}</div>
        </div>
        <span className={'format-tag format-tag--' + branch.format}>{branch.formatLabel}</span>
      </div>
      <div className="branch-card__body">
        <div className="branch-card__row"><Icon.pin /><span>{branch.address}</span></div>
        <div className="branch-card__row"><Icon.clock /><span>{branch.hours}</span></div>
        <div className="branch-card__row"><Icon.phone /><a href={`tel:${branch.phone.replace(/\s/g,'')}`}>{branch.phone}</a></div>
      </div>
      <div className="branch-card__deeplinks">
        {items.map(it => it.url ? (
          <a key={it.key} className="deeplink" href={it.url} target="_blank" rel="noopener" onClick={e => e.stopPropagation()}>
            <span className="deeplink__icon">{it.icon}</span>
            <span>{it.label}</span>
          </a>
        ) : (
          <span key={it.key} className="deeplink deeplink--disabled" title="Not available on this platform">
            <span className="deeplink__icon" style={{ opacity: 0.4 }}>{it.icon}</span>
            <span>—</span>
          </span>
        ))}
      </div>
    </div>
  );
}

Object.assign(window, { Icon, ReviewBadge, Navbar, Footer, DishCard, BranchCard });
