/* ============ HERMANITO · data + helpers ============ */

const BRAND = {
  name: 'Hermanito',
  tagline: 'Empanadas Artesanais',
  whatsapp: '5519971061610',          // wa.me number
  whatsappShow: '(19) 97106-1610',
  city: 'Hortolândia',
  region: 'SP',
  domain: 'elhermanito.com.br',
};

const MENU = [
  {
    id: 'carne', name: 'Carne', sub: 'Tradicional', price: 9,
    desc: 'A clássica argentina: carne, ovo, azeitona e muito sabor.',
    kcal: 270, protein: 15, img: 'assets/emp-carne.png', tag: 'A mais pedida',
  },
  {
    id: 'pollo', name: 'Pollo', sub: 'Tradicional', price: 9,
    desc: 'Frango suculento assado ao forno, bem temperado.',
    kcal: 240, protein: 17, img: 'assets/emp-pollo.png',
  },
  {
    id: 'humita', name: 'Humita', sub: 'Vegetariana', price: 9,
    desc: 'Milho cremoso com molho bechamel bem temperado.',
    kcal: 230, protein: 6, img: 'assets/emp-humita.png', veg: true,
  },
];

const BY_ID = Object.fromEntries(MENU.map(m => [m.id, m]));

/* ---- hours / scheduling ---- */
const OPEN_DAYS = [4, 5, 6];          // Thu, Fri, Sat (Date.getDay: Sun=0)
const ORDER_CUTOFF_H = 19;            // pedidos até 19h
const DELIVERY_FROM = '19h30';
const DAY_FULL  = ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado'];
const DAY_NIGHT = ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado'];

function pad2(n){ return String(n).padStart(2,'0'); }
function fmtDM(d){ return pad2(d.getDate()) + '/' + pad2(d.getMonth()+1); }

function isOpenNowReal(){
  const n = new Date();
  return OPEN_DAYS.includes(n.getDay()) && n.getHours() < ORDER_CUTOFF_H;
}
// sim: 'auto' | 'open' | 'closed'
function resolveOpen(sim){
  if (sim === 'open') return true;
  if (sim === 'closed') return false;
  return isOpenNowReal();
}

// Upcoming delivery slots (next `count` open nights, incl. today if still before cutoff)
function nextSlots(count = 3){
  const n = new Date();
  const out = [];
  const d = new Date(n.getFullYear(), n.getMonth(), n.getDate());
  for (let i = 0; i < 21 && out.length < count; i++){
    const day = d.getDay();
    if (OPEN_DAYS.includes(day)){
      const isToday = i === 0;
      const stillToday = isToday && n.getHours() < ORDER_CUTOFF_H;
      if (!isToday || stillToday){
        out.push({
          key: d.toISOString().slice(0,10),
          dayName: DAY_NIGHT[day],
          date: new Date(d),
          dm: fmtDM(d),
          isToday: stillToday,
          label: (stillToday ? 'Hoje' : DAY_NIGHT[day]) + ' à noite · ' + fmtDM(d),
        });
      }
    }
    d.setDate(d.getDate() + 1);
  }
  return out;
}

/* ---- money ---- */
function brl(v){
  return 'R$ ' + v.toFixed(2).replace('.', ',');
}

/* ---- WhatsApp order message ---- */
function buildOrderMessage(cart, form, totals){
  const L = [];
  L.push('Olá, *Hermanito*! 🇦🇷 Quero fazer um pedido:');
  L.push('');
  L.push('*🥟 Empanadas*');
  MENU.forEach(m => {
    const q = cart[m.id];
    if (q > 0) L.push(`• ${q}x ${m.name} ${m.sub} — ${brl(q * m.price)}`);
  });
  L.push('');
  L.push(`*Subtotal:* ${brl(totals.subtotal)}`);
  L.push(`*Entrega:* ${totals.fee > 0 ? brl(totals.fee) : 'a combinar / grátis'}`);
  L.push(`*Total:* ${brl(totals.total)}`);
  L.push('');
  L.push(`*Quando:* ${form.when}`);
  L.push(`*Tipo:* ${form.mode === 'retirada' ? 'Retirada' : 'Entrega'}`);
  L.push('');
  L.push('*Meus dados*');
  L.push(`Nome: ${form.name}`);
  L.push(`Telefone: ${form.phone}`);
  if (form.mode !== 'retirada'){
    L.push(`Endereço: ${form.address}`);
    L.push(`Bairro: ${form.district}`);
    L.push(`Cidade: ${BRAND.city}`);
    if (form.ref) L.push(`Referência: ${form.ref}`);
  }
  L.push('');
  L.push(`*Pagamento:* ${form.payment === 'pix' ? 'Pix' : 'Dinheiro'}`);
  if (form.payment === 'dinheiro' && form.change) L.push(`Troco para: ${form.change}`);
  if (form.notes) { L.push(''); L.push(`*Obs:* ${form.notes}`); }
  L.push('');
  L.push('Obrigado! ❤️');
  return L.join('\n');
}

function waLink(text){
  return 'https://wa.me/' + BRAND.whatsapp + '?text=' + encodeURIComponent(text);
}

/* ---- inline icon set (stroke uses currentColor) ---- */
function Icon({ name, style }){
  const p = { fill:'none', stroke:'currentColor', strokeWidth:1.8, strokeLinecap:'round', strokeLinejoin:'round' };
  const paths = {
    cart: <><circle cx="9" cy="20" r="1.4"/><circle cx="18" cy="20" r="1.4"/><path d="M2 3h3l2.2 12.4a1.5 1.5 0 0 0 1.5 1.2h8.4a1.5 1.5 0 0 0 1.5-1.2L21 7H6"/></>,
    whatsapp: <path d="M12 3a9 9 0 0 0-7.7 13.6L3 21l4.6-1.2A9 9 0 1 0 12 3Zm4.6 12.3c-.2.6-1.1 1.1-1.6 1.1-.4.1-.9.1-1.5-.1a10 10 0 0 1-5.4-4.8c-.4-.7-.6-1.4-.6-2 0-.6.3-1.2.7-1.5.2-.2.4-.2.6-.2h.4c.2 0 .4 0 .6.5l.7 1.6c0 .2 0 .3-.1.5l-.4.5c-.2.2-.3.3-.1.6.3.5.8 1.2 1.4 1.7.7.6 1.3.8 1.6.9.2 0 .4 0 .5-.2l.6-.7c.2-.2.3-.2.6-.1l1.5.7c.3.2.4.2.4.4.1.2.1.6 0 .9Z"/>,
    flame: <path d="M12 3s5 4 5 9a5 5 0 0 1-10 0c0-1.6.6-2.8 1.3-3.7.4 1 .9 1.4 1.5 1.4C11 9.7 9.5 7 12 3Z"/>,
    bolt: <path d="M13 3 5 13h5l-1 8 8-11h-5l1-7Z"/>,
    oven: <><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M3 9h18M7 6.5h.01M11 6.5h.01"/><rect x="7" y="12" width="10" height="5" rx="1"/></>,
    rolling: <><path d="M4 14 14 4l2 2L6 16z"/><circle cx="18" cy="6" r="2"/><path d="M3 20l3-3"/></>,
    leaf: <path d="M11 20A7 7 0 0 1 4 13c0-5 5-9 16-9 0 11-4 16-9 16ZM4 20c2-6 5-8 9-9"/>,
    heart: <path d="M12 20s-7-4.3-9.3-8.5C1.2 8.6 2.6 5.5 6 5.5c2 0 3.2 1.4 4 2.5.8-1.1 2-2.5 4-2.5 3.4 0 4.8 3.1 3.3 6C19 15.7 12 20 12 20Z"/>,
    moon: <path d="M20 13a8 8 0 1 1-9-9 6.5 6.5 0 0 0 9 9Z"/>,
    star: <path d="m12 4 2 5 5 .4-3.8 3.3 1.2 5L12 15l-4.4 2.7 1.2-5L5 9.4 10 9z"/>,
    pin: <><path d="M12 21s-6-5.2-6-10a6 6 0 0 1 12 0c0 4.8-6 10-6 10Z"/><circle cx="12" cy="11" r="2.2"/></>,
    clock: <><circle cx="12" cy="12" r="8.5"/><path d="M12 7.5V12l3 2"/></>,
    check: <path d="m4 12 5 5L20 6"/>,
    wheat: <path d="M12 3v18M12 7c-2-1-3 .5-3 2 1.5.5 3-.5 3-2Zm0 0c2-1 3 .5 3 2-1.5.5-3-.5-3-2Zm0 5c-2-1-3 .5-3 2 1.5.5 3-.5 3-2Zm0 0c2-1 3 .5 3 2-1.5.5-3-.5-3-2Z"/>,
  };
  return (
    <svg viewBox="0 0 24 24" {...p} style={style} aria-hidden="true">
      {paths[name] || null}
    </svg>
  );
}

Object.assign(window, {
  BRAND, MENU, BY_ID, OPEN_DAYS, ORDER_CUTOFF_H, DELIVERY_FROM,
  DAY_FULL, isOpenNowReal, resolveOpen, nextSlots, brl,
  buildOrderMessage, waLink, Icon, fmtDM,
});
