// Shared data for Azra Markt website — i18n-ready.
// Strings sind in components/i18n.jsx unter den Namensräumen
// `depts.<id>`, `deals.<id>`, `tags.<id>`, `units.<id>`, `deptNames.<id>`
// abgelegt. Hier nur strukturelle/numerische Daten.

const DEPARTMENTS = [
  { id: 'metzgerei', icon: 'cleaver', image: 'uploads/sortiment-metzgerei.jpg' },
  { id: 'obst',      icon: 'leaf',   image: 'uploads/sortiment-obst.jpg' },
  { id: 'backwaren', icon: 'wheat',  image: 'uploads/sortiment-backwaren.jpg' },
  { id: 'kaese',     icon: 'cheese', image: 'uploads/sortiment-kaese.jpg' },
  { id: 'oliven',    icon: 'olive',  image: 'uploads/sortiment-oliven.jpg' },
  { id: 'gewuerze',  icon: 'spice',  image: 'uploads/sortiment-gewuerze.jpg' },
  { id: 'sueßes',    icon: 'sweet',  image: 'uploads/sortiment-suesses.jpg' },
  { id: 'tk',        icon: 'box',    image: 'uploads/sortiment-tk.jpg' },
];

const DEALS = [
  { id: 1,  key: 'kalbknochen',       unitKey: 'perKg',  price: '9,99',  tagKey: 'hauspreis', deptKey: 'metzgerei', color: '#7A2E1F', image: 'uploads/angebot-kalbknochen.jpg' },
  { id: 2,  key: 'haehnchenschenkel', unitKey: 'kg4',    price: '10,99', tagKey: 'aktion',    deptKey: 'metzgerei', color: '#9C3E2C', image: 'uploads/angebot-haehnchenschenkel.jpg' },
  { id: 3,  key: 'haehnchenkeule',    unitKey: 'kg3',    price: '10,99', tagKey: 'aktion',    deptKey: 'metzgerei', color: '#9C3E2C', image: 'uploads/angebot-haehnchenkeule.jpg' },
  { id: 4,  key: 'haehnchenfluegel',  unitKey: 'kg3',    price: '11,99', tagKey: 'aktion',    deptKey: 'metzgerei', color: '#7A2E1F', image: 'uploads/angebot-haehnchenfluegel.jpg' },
  { id: 5,  key: 'lammrippchen',      unitKey: 'kg1',    price: '10,99', tagKey: 'frisch',    deptKey: 'metzgerei', color: '#7A2E1F', image: 'uploads/angebot-lammrippchen.jpg' },
  { id: 6,  key: 'lammhals',          unitKey: 'kg1',    price: '12,99', tagKey: 'frisch',    deptKey: 'metzgerei', color: '#9C3E2C', image: 'uploads/angebot-lammhals.jpg' },
  { id: 7,  key: 'kalbbeinscheibe',   unitKey: 'perKg',  price: '12,99', tagKey: 'theke',     deptKey: 'metzgerei', color: '#7A2E1F', image: 'uploads/angebot-kalbbeinscheibe.jpg' },
  { id: 8,  key: 'rindgulasch',       unitKey: 'kg2',    price: '22,99', tagKey: 'hauspreis', deptKey: 'metzgerei', color: '#9C3E2C', image: 'uploads/angebot-rindgulasch.jpg' },
  { id: 9,  key: 'kombikaese',        unitKey: 'g820',   price: '2,99',  tagKey: 'frisch',    deptKey: 'kaese',     color: '#5C6B3A', image: 'uploads/angebot-kombikaese.jpg' },
  { id: 10, key: 'maiskorn',          unitKey: 'g800x3', price: '3,99',  tagKey: 'vorrat',    deptKey: 'trocken',   color: '#A6802E', image: 'uploads/angebot-maiskorn.jpg' },
  { id: 11, key: 'rotelinsen',        unitKey: 'g800x3', price: '4,99',  tagKey: 'vorrat',    deptKey: 'trocken',   color: '#A6802E', image: 'uploads/angebot-rotelinsen.jpg' },
  { id: 12, key: 'tomatenmark',       unitKey: 'g820',   price: '1,99',  tagKey: 'vorrat',    deptKey: 'trocken',   color: '#A6802E', image: 'uploads/angebot-tomatenmark.jpg' },
];

// Trend / „Nur bei Azra" — hypte Produkte, exklusiv im Laden.
// Name kommt aus i18n.trendNames.<key> (oder hier inline als Fallback).
// Section blendet sich aus, wenn das Array leer ist.
const TRENDS = [
  { id: 't1', key: 'matchaLatte',    price: '5,99', image: 'uploads/trend-matcha-latte.png' },
  { id: 't2', key: 'creamyJalapeno', price: '2,99', image: 'uploads/trend-creamy-jalapeno.png', subKey: 'moreFlavors' },
  { id: 't3', key: 'pistazienCreme', price: '2,99', oldPrice: '4,49', image: 'uploads/trend-pistazien-creme.png', subKey: 'premiumPick' },
];

// Öffnungszeiten — Tagnamen kommen aus i18n.hours.days[dayIndex]
const HOURS = [
  { o: '08:00', c: '20:00' }, // Montag
  { o: '08:00', c: '20:00' },
  { o: '08:00', c: '20:00' },
  { o: '08:00', c: '20:00' },
  { o: '08:00', c: '20:00' },
  { o: '08:00', c: '20:00' }, // Samstag
  { o: null,    c: null    }, // Sonntag
];

// — Hessische Feiertage —
// Ostersonntag nach Meeus/Jones/Butcher (Anonymous Gregorian Algorithm)
function _easterSunday(year) {
  const a = year % 19;
  const b = Math.floor(year / 100);
  const c = year % 100;
  const d = Math.floor(b / 4);
  const e = b % 4;
  const f = Math.floor((b + 8) / 25);
  const g = Math.floor((b - f + 1) / 3);
  const h = (19*a + b - d - g + 15) % 30;
  const i = Math.floor(c / 4);
  const k = c % 4;
  const l = (32 + 2*e + 2*i - h - k) % 7;
  const mm = Math.floor((a + 11*h + 22*l) / 451);
  const month = Math.floor((h + l - 7*mm + 114) / 31); // 3=März, 4=April
  const day = ((h + l - 7*mm + 114) % 31) + 1;
  return new Date(year, month - 1, day);
}
function _addDays(date, n) {
  const d = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  d.setDate(d.getDate() + n);
  return d;
}
function _ymd(date) {
  return date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate();
}
const _HOLIDAY_CACHE = {};
function getHessianHolidays(year) {
  if (_HOLIDAY_CACHE[year]) return _HOLIDAY_CACHE[year];
  const easter = _easterSunday(year);
  const list = [
    { date: new Date(year, 0, 1),    key: 'neujahr' },
    { date: _addDays(easter, -2),    key: 'karfreitag' },
    { date: _addDays(easter, 1),     key: 'ostermontag' },
    { date: new Date(year, 4, 1),    key: 'tagDerArbeit' },
    { date: _addDays(easter, 39),    key: 'christiHimmelfahrt' },
    { date: _addDays(easter, 50),    key: 'pfingstmontag' },
    { date: _addDays(easter, 60),    key: 'fronleichnam' },
    { date: new Date(year, 9, 3),    key: 'tagDerDeutschenEinheit' },
    { date: new Date(year, 11, 25),  key: 'weihnachten1' },
    { date: new Date(year, 11, 26),  key: 'weihnachten2' },
  ];
  list.sort((a,b) => a.date - b.date);
  _HOLIDAY_CACHE[year] = list;
  return list;
}
function getHolidayForDate(date) {
  const list = getHessianHolidays(date.getFullYear());
  const target = _ymd(date);
  for (const h of list) if (_ymd(h.date) === target) return h;
  return null;
}
function getUpcomingHolidays(from, count = 3) {
  const start = new Date(from.getFullYear(), from.getMonth(), from.getDate());
  const out = [];
  for (let y = start.getFullYear(); y <= start.getFullYear()+1 && out.length < count; y++) {
    for (const h of getHessianHolidays(y)) {
      if (h.date >= start) out.push(h);
      if (out.length >= count) break;
    }
  }
  return out;
}

// useOpenStatus — liefert { open, label } in der aktiven Sprache
function useOpenStatus() {
  const { t } = useT();
  const [now, setNow] = React.useState(new Date());
  React.useEffect(() => {
    const tk = setInterval(() => setNow(new Date()), 30000);
    return () => clearInterval(tk);
  }, []);
  const day = (now.getDay() + 6) % 7; // mon=0
  const today = HOURS[day];
  const m = now.getHours()*60 + now.getMinutes();
  const parse = (s) => s ? (parseInt(s.split(':')[0])*60 + parseInt(s.split(':')[1])) : null;
  const o = parse(today.o), c = parse(today.c);
  const todayHoliday = getHolidayForDate(now);
  if (!todayHoliday && today.o && m >= o && m < c) {
    const left = c - m;
    if (left <= 30) return { open: true, label: t('open.closesInMin', { min: left }) };
    return { open: true, label: t('open.untilHHMM', { hhmm: today.c }) };
  }
  // Geschlossen — nächsten Öffnungstag suchen (Feiertage überspringen)
  const startToday = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  let nextDate = null;
  let nextOpenStr = null;
  for (let i = 1; i <= 21; i++) {
    const cand = new Date(startToday.getFullYear(), startToday.getMonth(), startToday.getDate() + i);
    const slot = HOURS[(cand.getDay() + 6) % 7];
    if (!slot.o) continue;
    if (getHolidayForDate(cand)) continue;
    nextDate = cand;
    nextOpenStr = slot.o;
    break;
  }
  const diffDays = nextDate ? Math.round((nextDate - startToday) / 86400000) : null;
  const nextWeekdayName = nextDate ? ((t('hours.days') || [])[(nextDate.getDay()+6)%7] || '') : '';

  if (todayHoliday) {
    const name = t('holidays.' + todayHoliday.key);
    if (diffDays === 1) return { open:false, label: t('open.holidayTomorrowAt', { name, hhmm: nextOpenStr }) };
    if (diffDays != null) return { open:false, label: t('open.holidayOnDayAt', { name, day: nextWeekdayName, hhmm: nextOpenStr }) };
    return { open:false, label: t('open.holidayToday', { name }) };
  }
  if (today.o && m < o) return { open:false, label: t('open.opensTodayAt', { hhmm: today.o }) };
  if (diffDays === 1) return { open:false, label: t('open.opensTomorrowAt', { hhmm: nextOpenStr }) };
  if (diffDays != null) return { open:false, label: t('open.opensOnDayAt', { day: nextWeekdayName, hhmm: nextOpenStr }) };
  return { open:false, label: t('common.closedNow') };
}

// Tiny inline SVG icon set (line icons, monoline)
const Icon = ({ name, size=28, stroke=1.4, className }) => {
  const p = { fill:'none', stroke:'currentColor', strokeWidth:stroke, strokeLinecap:'round', strokeLinejoin:'round' };
  const ic = {
    cleaver: <g {...p}><path d="M4 17 L4 13 L16 13 L20 9 L20 17 Z"/><path d="M4 17 L20 17"/><path d="M16 13 L20 13"/></g>,
    leaf:    <g {...p}><path d="M5 19 C 5 11, 11 5, 19 5 C 19 13, 13 19, 5 19 Z"/><path d="M5 19 L 14 10"/></g>,
    wheat:   <g {...p}><path d="M12 21 L12 5"/><path d="M12 9 C 9 8, 7 6, 7 4 C 9 4, 11 6, 12 9 Z"/><path d="M12 9 C 15 8, 17 6, 17 4 C 15 4, 13 6, 12 9 Z"/><path d="M12 14 C 9 13, 7 11, 7 9 C 9 9, 11 11, 12 14 Z"/><path d="M12 14 C 15 13, 17 11, 17 9 C 15 9, 13 11, 12 14 Z"/></g>,
    cheese:  <g {...p}><path d="M3 17 L21 11 L21 17 Z"/><circle cx="9" cy="15" r=".7" fill="currentColor"/><circle cx="14" cy="14" r=".7" fill="currentColor"/><circle cx="17" cy="13" r=".7" fill="currentColor"/></g>,
    olive:   <g {...p}><ellipse cx="12" cy="13" rx="4" ry="6"/><path d="M12 7 C 14 5, 17 4, 19 5"/></g>,
    spice:   <g {...p}><path d="M8 4 L16 4 L16 8 L8 8 Z"/><path d="M9 8 L9 20 L15 20 L15 8"/><path d="M11 12 L11 12.01 M13 14 L13 14.01 M11 16 L11 16.01"/></g>,
    sweet:   <g {...p}><path d="M5 10 C 5 7, 8 5, 12 5 C 16 5, 19 7, 19 10 L 19 11 L 5 11 Z"/><path d="M4 13 L20 13 L18 19 L6 19 Z"/></g>,
    box:     <g {...p}><path d="M4 8 L12 4 L20 8 L20 18 L12 22 L4 18 Z"/><path d="M4 8 L12 12 L20 8"/><path d="M12 12 L12 22"/></g>,
    phone:   <g {...p}><path d="M5 4 L9 4 L11 10 L8 12 C 9 15, 11 17, 14 18 L16 15 L22 17 L22 21 C 13 21, 5 13, 5 4 Z"/></g>,
    pin:     <g {...p}><path d="M12 22 C 7 16, 4 13, 4 9 C 4 5, 8 2, 12 2 C 16 2, 20 5, 20 9 C 20 13, 17 16, 12 22 Z"/><circle cx="12" cy="9" r="2.5"/></g>,
    clock:   <g {...p}><circle cx="12" cy="12" r="9"/><path d="M12 7 L12 12 L16 14"/></g>,
    arrow:   <g {...p}><path d="M5 12 L19 12"/><path d="M14 7 L19 12 L14 17"/></g>,
    plus:    <g {...p}><path d="M12 5 L12 19 M5 12 L19 12"/></g>,
    minus:   <g {...p}><path d="M5 12 L19 12"/></g>,
    close:   <g {...p}><path d="M6 6 L18 18 M18 6 L6 18"/></g>,
    menu:    <g {...p}><path d="M4 7 L20 7 M4 12 L20 12 M4 17 L20 17"/></g>,
    star:    <g {...p}><path d="M12 4 L14.5 9.5 L20 10.3 L16 14.3 L17 20 L12 17.3 L7 20 L8 14.3 L4 10.3 L9.5 9.5 Z"/></g>,
    halal:   <g {...p}><circle cx="12" cy="12" r="9"/><path d="M7 14 C 8 11, 11 11, 12 13 C 13 11, 16 11, 17 14"/><path d="M9 9 L9 9.01 M15 9 L15 9.01"/></g>,
    tray:    <g {...p}><path d="M3 10 L21 10"/><path d="M5 10 C 5 6, 8 4, 12 4 C 16 4, 19 6, 19 10"/><path d="M5 10 L7 20 L17 20 L19 10"/></g>,
    bag:     <g {...p}><path d="M6 8 L18 8 L19 21 L5 21 Z"/><path d="M9 8 C 9 5, 10 3, 12 3 C 14 3, 15 5, 15 8"/></g>,
    chev_l:  <g {...p}><path d="M15 6 L9 12 L15 18"/></g>,
    chev_r:  <g {...p}><path d="M9 6 L15 12 L9 18"/></g>,
    mail:    <g {...p}><rect x="3" y="6" width="18" height="13" rx="1.5"/><path d="M3 7 L12 13 L21 7"/></g>,
    route:   <g {...p}><circle cx="6" cy="6" r="2.4"/><circle cx="18" cy="18" r="2.4"/><path d="M6 8 L6 14 C 6 16, 8 17, 10 17 L14 17"/></g>,
    globe:   <g {...p}><circle cx="12" cy="12" r="9"/><path d="M3 12 L21 12"/><path d="M12 3 C 15 7, 15 17, 12 21"/><path d="M12 3 C 9 7, 9 17, 12 21"/></g>,
  };
  return <svg width={size} height={size} viewBox="0 0 24 24" aria-hidden="true" className={className}>{ic[name] || ic.star}</svg>;
};

Object.assign(window, { DEPARTMENTS, DEALS, HOURS, TRENDS, useOpenStatus, Icon, getHessianHolidays, getHolidayForDate, getUpcomingHolidays });
