/* Everbloom landing - phone frame, app-UI screens, faux QR, local Icon helper */

/* ---- Local Lucide icon helper (self-contained; no DS bundle needed) ---- */
function ebToPascal(n){ return String(n).replace(/(^\w|-\w)/g, s => s.replace('-','').toUpperCase()); }
function EBIcon({ name, size, strokeWidth = 1.7, style, className = '', fill = 'none' }) {
  const L = typeof window !== 'undefined' ? window.lucide : null;
  let html = null;
  if (L && L.icons) {
    const node = L.icons[ebToPascal(name)] || L.icons[name];
    if (node && Array.isArray(node) && Array.isArray(node[2])) {
      const inner = node[2].map(([tag, attrs]) =>
        '<' + tag + ' ' + Object.entries(attrs || {}).map(([k,v]) => `${k}="${v}"`).join(' ') + ' />'
      ).join('');
      html = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="${fill}" stroke="currentColor" stroke-width="${strokeWidth}" stroke-linecap="round" stroke-linejoin="round">${inner}</svg>`;
    }
  }
  const s = { display: 'inline-flex', lineHeight: 0, flex: 'none',
    width: size != null ? (typeof size === 'number' ? size+'px' : size) : '1em',
    height: size != null ? (typeof size === 'number' ? size+'px' : size) : '1em', ...style };
  return <span className={('eb-i '+className).trim()} style={s} aria-hidden="true"
    dangerouslySetInnerHTML={html ? { __html: html } : undefined} />;
}

/* ---- Faux QR (deterministic) ---- */
function FauxQR() {
  const N = 11;
  const cells = [];
  const isFinder = (r, c) => {
    const inTL = r < 3 && c < 3, inTR = r < 3 && c > N - 4, inBL = r > N - 4 && c < 3;
    return inTL || inTR || inBL;
  };
  for (let r = 0; r < N; r++) for (let c = 0; c < N; c++) {
    let on;
    if (isFinder(r, c)) {
      const lr = r % (N - 3 <= r ? 1 : 100); // not used
      // draw a ring: border on, center on, gap off
      const rr = r < 3 ? r : r - (N - 3);
      const cc = c < 3 ? c : c - (N - 3);
      on = !(rr === 1 && cc === 1) ? (rr === 0 || rr === 2 || cc === 0 || cc === 2) : true;
      on = (rr === 0 || rr === 2 || cc === 0 || cc === 2 || (rr === 1 && cc === 1));
    } else {
      on = ((r * 7 + c * 5 + r * c * 3) % 3 === 0) || ((r + c) % 4 === 0 && (r * c) % 2 === 0);
    }
    cells.push(on);
  }
  return (
    <div className="appui-qr">
      <div className="appui-qr-grid">
        {cells.map((on, i) => <i key={i} style={{ visibility: on ? 'visible' : 'hidden' }} />)}
      </div>
    </div>
  );
}

/* ---- Phone frame ---- */
function PhoneFrame({ children }) {
  return (
    <div className="lp-phone">
      <div className="lp-phone-notch" />
      <div className="lp-phone-screen">{children}</div>
    </div>
  );
}

function TabBar({ active = 'photos' }) {
  const tabs = [['image','photos'],['mail','invites'],['users','rsvp'],['sparkles','ai']];
  return (
    <div className="appui-tabbar">
      {tabs.map(([ic, key]) => (
        <EBIcon key={key} name={ic} className={active === key ? 'on' : ''} style={{ color: active===key ? 'var(--ink)' : 'var(--ink-3)' }} />
      ))}
    </div>
  );
}

/* ---- Screens ---- */
const TONES = ['ph-a','ph-b','ph-c','ph-d','ph-e','ph-f','ph-b','ph-c','ph-a','ph-e','ph-d','ph-f'];

function ScreenGallery() {
  const { t } = React.useContext(window.EB_LangContext);
  const a = t.appui;
  return (
    <div className="appui">
      <div className="appui-top">
        <div>
          <div className="appui-title">{a.you}</div>
          <div className="appui-sub">{a.ph} · 642 foto</div>
        </div>
        <EBIcon name="heart" style={{ color: 'var(--terracotta)', fontSize: '18px' }} />
      </div>
      <div className="appui-body">
        <div className="appui-photogrid">
          {TONES.map((tone, i) => <div key={i} className={'tile ph-tone ' + tone} />)}
          <img className="appui-gallery-photo" src="assets/gallery-photos.png" alt=""
            onError={(e) => { e.target.style.display = 'none'; }} />
        </div>
      </div>
      <div className="appui-fab"><EBIcon name="plus" />{a.upload}</div>
      <TabBar active="photos" />
    </div>
  );
}

function ScreenQR() {
  const { t } = React.useContext(window.EB_LangContext);
  const a = t.appui;
  return (
    <div className="appui">
      <div className="appui-qr-wrap">
        <FauxQR />
        <div className="appui-qr-cap">{a.scan}</div>
        <div className="appui-qr-note">{a.scanNote}</div>
        <div style={{ display:'flex', gap:'6px', marginTop:'4px' }}>
          <span className="appui-chip" style={{ background:'var(--gold-soft)', color:'var(--gold-deep)' }}>Cerimonia</span>
          <span className="appui-chip" style={{ background:'var(--sage-soft)', color:'var(--sage-deep)' }}>Cena</span>
        </div>
      </div>
    </div>
  );
}

function ScreenRSVP() {
  const { t } = React.useContext(window.EB_LangContext);
  const a = t.appui;
  return (
    <div className="appui">
      <div className="appui-top">
        <div className="appui-title" style={{ fontSize: '15px' }}>RSVP</div>
        <EBIcon name="check-circle" style={{ color: 'var(--sage-deep)', fontSize:'16px' }} />
      </div>
      <div className="appui-body">
        <div className="appui-rsvp">
          <div className="appui-seg">
            <div className="opt sel"><EBIcon name="check" style={{fontSize:'12px', marginRight:'4px'}} />{a.attending}</div>
            <div className="opt">{a.declining}</div>
          </div>
          <div>
            <div className="q" style={{ marginBottom:'5px' }}>{a.guests}</div>
            <div className="appui-field"><span>2 {a.guests.toLowerCase()}</span><EBIcon name="chevron-down" style={{fontSize:'13px', color:'var(--ink-3)'}} /></div>
          </div>
          <div>
            <div className="q" style={{ marginBottom:'5px' }}>{a.diet}</div>
            <div className="appui-chiprow">
              <span className="appui-chip">Vegetariano</span>
              <span className="appui-chip">Senza glutine</span>
              <span className="appui-chip" style={{ background:'var(--gold-soft)', color:'var(--gold-deep)' }}>+ Nota</span>
            </div>
          </div>
          <div className="appui-fab" style={{ position:'static', transform:'none', marginTop:'8px', justifyContent:'center' }}>
            {t.appui.attending} <EBIcon name="arrow-right" />
          </div>
        </div>
      </div>
    </div>
  );
}

function ScreenTables() {
  const Seats = ({ n, filled }) => (
    <>{Array.from({ length: n }).map((_, i) => {
      const ang = (i / n) * Math.PI * 2 - Math.PI / 2;
      const R = 35;
      const x = 50 + Math.cos(ang) * R, y = 50 + Math.sin(ang) * R;
      return <span key={i} className={'seat' + (i >= filled ? ' empty' : '')}
        style={{ left: x + '%', top: y + '%', transform: 'translate(-50%,-50%)' }} />;
    })}</>
  );
  const Table = ({ n, seats, filled, full, drop, style }) => (
    <div className={'appui-table' + (full ? ' full' : '') + (drop ? ' drop' : '')} style={style}>
      <div className="tnum">{n}</div>
      <div className="tcount">{filled}/{seats}</div>
      <Seats n={seats} filled={filled} />
    </div>
  );
  return (
    <div className="appui">
      <div className="appui-top">
        <div className="appui-title" style={{ fontSize: '15px' }}>Tavoli</div>
        <div style={{ display: 'flex', gap: '10px', color: 'var(--ink-3)' }}>
          <EBIcon name="search" style={{ fontSize: '14px' }} />
          <EBIcon name="download" style={{ fontSize: '14px' }} />
        </div>
      </div>
      <div className="appui-body">
        <div className="appui-tables">
          <Table n="1" seats={8} filled={8} full style={{ width: '82px', height: '82px', left: '4px', top: '2px' }} />
          <Table n="2" seats={6} filled={6} full style={{ width: '70px', height: '70px', right: '0', top: '16px' }} />
          <Table n="3" seats={8} filled={5} drop style={{ width: '84px', height: '84px', left: '16px', top: '100px' }} />
          <Table n="4" seats={6} filled={2} style={{ width: '70px', height: '70px', right: '4px', top: '116px' }} />
          <div className="appui-guestchip" style={{ left: '86px', top: '86px', transform: 'rotate(-3deg)', zIndex: 4 }}>
            <span className="av" />Zia Anna
          </div>
          <div className="appui-tray">
            <div className="appui-tray-h">Da assegnare · 3</div>
            <div className="appui-tray-row">
              <span className="appui-traychip"><span className="av" style={{ background: 'linear-gradient(135deg,var(--sage),var(--gold))' }} />Marco</span>
              <span className="appui-traychip"><span className="av" style={{ background: 'linear-gradient(135deg,var(--blush),var(--terracotta))' }} />Sofia</span>
              <span className="appui-traychip"><span className="av" style={{ background: 'linear-gradient(135deg,var(--gold),var(--terracotta))' }} />+1</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

function ScreenLive({ tone = 'ph-f' }) {
  const { t } = React.useContext(window.EB_LangContext);
  return (
    <div className="appui">
      <div className="appui-live">
        <div className={'photo ph-tone ' + tone} />
        <div className="badge"><span className="pulse" />{t.appui.live}</div>
        <div style={{ position:'absolute', bottom:'16px', left:'14px', right:'14px', color:'#fff', zIndex:2 }}>
          <div style={{ fontFamily:'var(--font-script)', fontSize:'26px', lineHeight:1 }}>{t.appui.you}</div>
          <div style={{ fontSize:'10px', opacity:0.9, marginTop:'2px' }}>642 foto · 38 video</div>
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { EBIcon, FauxQR, PhoneFrame, TabBar, ScreenGallery, ScreenQR, ScreenRSVP, ScreenTables, ScreenLive });
