// data.jsx — product catalog + helpers
// All prices in EUR (whole numbers). Imagery uses placeholders.

const PRODUCTS = [
  // ── SKINCARE ───────────────────────────────────────────────
  {
    id: 'sk-stick-50',
    cat: 'skincare',
    kind: 'skin',
    name: 'velvet face stick',
    sub: 'invisible mineral filter · 22 g',
    spf: 'SPF50',
    price: 38,
    ph: 'tonal',
    phLabel: 'product still\nface stick · matte cream'
  },
  {
    id: 'sk-mist-30',
    cat: 'skincare',
    kind: 'skin',
    name: 'sun veil dry oil',
    sub: 'lightweight body mist · 150 ml',
    spf: 'SPF30',
    price: 44,
    ph: 'tonal',
    phLabel: 'product still\ndry oil mist · amber glass'
  },
  {
    id: 'sk-gel',
    cat: 'skincare',
    kind: 'skin',
    name: 'quiet hour gel',
    sub: 'after-sun serum · aloe + niacinamide · 100 ml',
    spf: null,
    price: 36,
    ph: 'tonal',
    phLabel: 'product still\ncooling gel · clear bottle'
  },
  {
    id: 'sk-butter',
    cat: 'skincare',
    kind: 'skin',
    name: 'afterglow butter',
    sub: 'rich body cream · shea + tamanu · 200 ml',
    spf: null,
    price: 42,
    ph: 'tonal',
    phLabel: 'product still\nbody butter · matte jar'
  },

  // ── BEACHWEAR — swimsuits ──────────────────────────────────
  {
    id: 'sw-maillot',
    cat: 'swim',
    kind: 'beach',
    name: 'shy maillot',
    sub: 'sculpted one-piece · black, ivory',
    price: 124,
    span: 5,
    swatches: ['#0e0c0a', '#efeae2'],
    sizes: ['xs','s','m','l','xl'],
    ph: 'dark',
    phLabel: 'campaign still\none-piece · model on sand'
  },
  {
    id: 'sw-triangle',
    cat: 'swim',
    kind: 'beach',
    name: 'nude triangle',
    sub: 'classic two-piece · 3 colorways',
    price: 98,
    span: 4,
    swatches: ['#0e0c0a','#efeae2','#d4a574'],
    sizes: ['xs','s','m','l','xl'],
    ph: 'tonal',
    phLabel: 'campaign still\ntriangle bikini'
  },
  {
    id: 'sw-bandeau',
    cat: 'swim',
    kind: 'beach',
    name: 'dune bandeau',
    sub: 'strapless · removable straps',
    price: 102,
    span: 3,
    swatches: ['#0e0c0a','#efeae2'],
    sizes: ['xs','s','m','l'],
    ph: 'tonal',
    phLabel: 'bandeau bikini\nstill life'
  },
  {
    id: 'sw-halter',
    cat: 'swim',
    kind: 'beach',
    name: 'lune halter',
    sub: 'high-neck halter · ribbed',
    price: 112,
    span: 4,
    swatches: ['#0e0c0a','#efeae2'],
    sizes: ['xs','s','m','l'],
    ph: 'dark',
    phLabel: 'halter top\ncampaign still'
  },
  {
    id: 'sw-brief',
    cat: 'swim',
    kind: 'beach',
    name: 'cipher high-cut',
    sub: 'leg-lengthening brief',
    price: 88,
    span: 4,
    swatches: ['#0e0c0a','#efeae2','#d4a574'],
    sizes: ['xs','s','m','l','xl'],
    ph: 'tonal',
    phLabel: 'high-cut brief\ndetail shot'
  },

  // ── BEACHWEAR — apparel ────────────────────────────────────
  {
    id: 'bw-dress',
    cat: 'apparel',
    kind: 'beach',
    name: 'hush dress',
    sub: 'beach dress · 4 prints',
    price: 168,
    span: 5,
    swatches: ['#0e0c0a','#efeae2','#d4a574','#7a7268'],
    sizes: ['xs','s','m','l'],
    ph: 'stripe',
    phLabel: 'hush dress\n4 prints · same cut'
  },
  {
    id: 'bw-pareo',
    cat: 'apparel',
    kind: 'beach',
    name: 'whisper pareo',
    sub: 'long sarong · light cotton',
    price: 78,
    span: 4,
    swatches: ['#0e0c0a','#efeae2','#d4a574'],
    sizes: ['one'],
    ph: 'tonal',
    phLabel: 'pareo\nfloating on body'
  },
  {
    id: 'bw-scarf',
    cat: 'apparel',
    kind: 'beach',
    name: 'foulard soleil',
    sub: 'silk-touch scarf · 90 × 90',
    price: 62,
    span: 3,
    swatches: ['#0e0c0a','#efeae2','#d4a574'],
    sizes: ['one'],
    ph: 'stripe',
    phLabel: 'scarf folded\nstill life'
  },
  {
    id: 'bw-shorts',
    cat: 'apparel',
    kind: 'beach',
    name: 'edit shorts',
    sub: 'compressive bike short',
    price: 74,
    span: 4,
    swatches: ['#0e0c0a','#efeae2'],
    sizes: ['xs','s','m','l','xl'],
    ph: 'dark',
    phLabel: 'bike short\ndetail crop'
  },
  {
    id: 'bw-tee',
    cat: 'apparel',
    kind: 'beach',
    name: 'underline tee',
    sub: 'oversized cotton tee',
    price: 64,
    span: 4,
    swatches: ['#0e0c0a','#efeae2','#d4a574'],
    sizes: ['xs','s','m','l','xl'],
    ph: 'stripe',
    phLabel: 'tee · oversized\nfit on model'
  },
  {
    id: 'bw-bag',
    cat: 'apparel',
    kind: 'beach',
    name: 'carryall pouch',
    sub: 'tonal toiletry bag',
    price: 48,
    span: 4,
    swatches: ['#0e0c0a','#d4a574'],
    sizes: ['one'],
    ph: 'tonal',
    phLabel: 'pouch · open\nshowing contents'
  },
];

const BUNDLES = [
  {
    id: 'bd-beach',
    name: 'beach day',
    eyebrow: 'edit · 03 pieces',
    items: [
      { ref:'sk-stick-50', label:'velvet face stick spf50', was:38 },
      { ref:'sk-mist-30',  label:'sun veil dry oil spf30',  was:44 },
      { ref:'bw-pareo',    label:'whisper pareo',           was:78 },
    ],
    price: 138,
    save: 22,
    featured: false,
  },
  {
    id: 'bd-after',
    name: 'after sun ritual',
    eyebrow: 'edit · 03 pieces',
    items: [
      { ref:'sk-gel',     label:'quiet hour gel',  was:36 },
      { ref:'sk-butter',  label:'afterglow butter',was:42 },
      { ref:'bw-tee',     label:'underline tee',   was:64 },
    ],
    price: 118,
    save: 24,
    featured: true,
  },
  {
    id: 'bd-full',
    name: 'the full drop',
    eyebrow: 'edit · 06 pieces',
    items: [
      { ref:'sk-stick-50',label:'velvet face stick spf50', was:38 },
      { ref:'sk-mist-30', label:'sun veil dry oil spf30',  was:44 },
      { ref:'sk-gel',     label:'quiet hour gel',           was:36 },
      { ref:'sk-butter',  label:'afterglow butter',         was:42 },
      { ref:'sw-triangle',label:'nude triangle',            was:98 },
      { ref:'bw-pareo',   label:'whisper pareo',            was:78 },
    ],
    price: 286,
    save: 50,
    featured: false,
  },
];

const findP = (id) => PRODUCTS.find(p => p.id === id);

// ── cart state hook ──────────────────────────────────────────
const CartCtx = React.createContext(null);

function useCart(){
  const v = React.useContext(CartCtx);
  if(!v) throw new Error('useCart outside provider');
  return v;
}

function CartProvider({ children }){
  const [items, setItems] = React.useState([]); // {id, qty, kind:'product'|'bundle', size?, color?, label, price, ph}
  const [open, setOpen] = React.useState(false);
  const [toast, setToast] = React.useState(null);

  const addProduct = React.useCallback((p, opts={}) => {
    setItems(curr => {
      const key = `${p.id}|${opts.size||''}|${opts.color||''}`;
      const idx = curr.findIndex(x => x.key === key);
      if (idx >= 0){
        const next = [...curr];
        next[idx] = { ...next[idx], qty: next[idx].qty + (opts.qty||1) };
        return next;
      }
      return [...curr, {
        key, id: p.id, kind:'product',
        label: p.name, sub: p.sub,
        price: p.price, qty: opts.qty || 1,
        size: opts.size || null, color: opts.color || null,
        ph: p.ph,
      }];
    });
    setToast(`${p.name} · added`);
    setTimeout(()=>setToast(null), 1800);
  }, []);

  const addBundle = React.useCallback((b) => {
    setItems(curr => {
      const key = `bundle|${b.id}`;
      const idx = curr.findIndex(x => x.key === key);
      if (idx >= 0){
        const next = [...curr];
        next[idx] = { ...next[idx], qty: next[idx].qty + 1 };
        return next;
      }
      return [...curr, {
        key, id: b.id, kind:'bundle',
        label: b.name, sub: `${b.items.length} pieces · bundle`,
        price: b.price, qty: 1,
        ph: 'tonal',
      }];
    });
    setToast(`${b.name} · bundle added`);
    setTimeout(()=>setToast(null), 1800);
  }, []);

  const addCustomBundle = React.useCallback((ids, price) => {
    if (!ids.length) return;
    setItems(curr => [...curr, {
      key: `custom|${Date.now()}`, id: 'custom', kind:'bundle',
      label: 'custom edit',
      sub: `${ids.length} pieces · build-your-own`,
      price, qty: 1,
      ph: 'tonal',
    }]);
    setToast('custom edit · added');
    setTimeout(()=>setToast(null), 1800);
  }, []);

  const setQty = (key, qty) => setItems(curr => qty<=0 ? curr.filter(x=>x.key!==key) : curr.map(x=>x.key===key?{...x, qty}:x));
  const remove = (key) => setItems(curr => curr.filter(x=>x.key!==key));
  const clear = () => setItems([]);

  const count = items.reduce((s,x)=>s+x.qty,0);
  const subtotal = items.reduce((s,x)=>s+x.qty*x.price,0);
  const shipping = subtotal>0 && subtotal<150 ? 9 : 0;
  const total = subtotal + shipping;

  return (
    <CartCtx.Provider value={{
      items, open, setOpen,
      addProduct, addBundle, addCustomBundle,
      setQty, remove, clear,
      count, subtotal, shipping, total,
      toast,
    }}>
      {children}
    </CartCtx.Provider>
  );
}

Object.assign(window, { PRODUCTS, BUNDLES, findP, CartProvider, useCart });
