// Product data + cart context

const PRODUCTS = [
  {
    id: 'forward',
    name: 'Forward Tee',
    code: 'FWD-01',
    price: 48,
    color: 'black',
    accent: 'cyan',
    desc: 'A clean heavy cotton tee inspired by the physical silence of underwater rugby. Made for players who know what happens below the surface.',
    weight: '240 GSM',
    fit: 'Athletic',
    fabric: '100% Heavy Cotton',
    sizes: ['S', 'M', 'L', 'XL', 'XXL'],
    colors: ['Black', 'Off White', 'Deep Ocean'],
    tag: 'BEST SELLER',
  },
  {
    id: 'no-breath',
    name: 'No Breath Club',
    code: 'NBC-02',
    price: 52,
    color: 'black',
    accent: 'red',
    desc: 'Heavyweight statement tee for the league. Front print: NO BREATH CLUB. Back: depth coordinates. Worn by those who train past the burn.',
    weight: '260 GSM',
    fit: 'Boxy',
    fabric: '100% Heavy Cotton',
    sizes: ['S', 'M', 'L', 'XL', 'XXL'],
    colors: ['Black', 'Signal Red'],
    tag: null,
  },
  {
    id: 'abyss',
    name: 'Abyss Training Tee',
    code: 'ABY-03',
    price: 45,
    color: 'cyan',
    desc: 'Performance training tee in deep ocean blue. Built for the pool deck, breathable enough for the gym. Fast-dry, low-friction shoulder seams.',
    weight: '180 GSM',
    fit: 'Athletic',
    fabric: '90% Cotton / 10% Elastane',
    sizes: ['XS', 'S', 'M', 'L', 'XL'],
    colors: ['Deep Ocean', 'Black'],
    tag: 'NEW',
  },
  {
    id: 'surface',
    name: 'Surface Is Optional',
    code: 'SIO-04',
    price: 56,
    color: 'black',
    accent: 'cyan',
    desc: 'Long-cut hoodie weight tee. Drop shoulder, ribbed neck. The mantra of the league printed across the back in tall caps.',
    weight: '280 GSM',
    fit: 'Oversized',
    fabric: '100% Heavy Cotton',
    sizes: ['S', 'M', 'L', 'XL'],
    colors: ['Black', 'Off White'],
    tag: 'DROP 01',
  },
  {
    id: 'goalie',
    name: 'Goalie Depth Tee',
    code: 'GLD-05',
    price: 50,
    color: 'black',
    desc: 'Cut for the keepers. Reinforced shoulder panels, longer hem, depth gauge graphic. Built to take contact, dry fast, last seasons.',
    weight: '240 GSM',
    fit: 'Athletic',
    fabric: '100% Heavy Cotton',
    sizes: ['M', 'L', 'XL', 'XXL'],
    colors: ['Black', 'Deep Ocean'],
    tag: null,
  },
  {
    id: 'pressure',
    name: 'Pressure Crew',
    code: 'PRS-06',
    price: 78,
    color: 'cyan',
    desc: 'Heavyweight crewneck. Silent on the pool deck, worn off it. Drop shoulder, brushed interior, depth coordinates at hem.',
    weight: '380 GSM',
    fit: 'Boxy',
    fabric: '80% Cotton / 20% Polyester',
    sizes: ['S', 'M', 'L', 'XL'],
    colors: ['Black', 'Deep Ocean'],
    tag: null,
  },
];

// Cart hook
const CartContext = React.createContext(null);

function CartProvider({ children }) {
  const [items, setItems] = React.useState([]);
  const [open, setOpen] = React.useState(false);

  const add = (product, size = 'M', color) => {
    setItems((prev) => {
      const key = `${product.id}-${size}-${color || product.colors[0]}`;
      const existing = prev.find((i) => i.key === key);
      if (existing) return prev.map((i) => i.key === key ? { ...i, qty: i.qty + 1 } : i);
      return [...prev, { key, product, size, color: color || product.colors[0], qty: 1 }];
    });
    setOpen(true);
  };
  const remove = (key) => setItems((prev) => prev.filter((i) => i.key !== key));
  const updateQty = (key, qty) => setItems((prev) => qty <= 0 ? prev.filter((i) => i.key !== key) : prev.map((i) => i.key === key ? { ...i, qty } : i));

  const count = items.reduce((s, i) => s + i.qty, 0);
  const subtotal = items.reduce((s, i) => s + i.qty * i.product.price, 0);

  return <CartContext.Provider value={{ items, add, remove, updateQty, count, subtotal, open, setOpen }}>{children}</CartContext.Provider>;
}

const useCart = () => React.useContext(CartContext);

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