// Curated bilingual catalog with 4 image variants per product (one per color),
// focused on modest, clothing-centric fashion photography.

const POOL = {
  'formal-dresses': [
    '1583846783214-7229a91b20ed',  // long elegant dress
    '1485462537746-965f33f7f6a7',  // modest covered dress
    '1604336891291-58f3f29c43d4',  // long dress
    '1571513722275-4b41940f54b8',  // evening gown (covered)
    '1612215047504-be5d33fb1502'   // modest fashion
  ],
  'casual-dresses': [
    '1485462537746-965f33f7f6a7',
    '1583846783214-7229a91b20ed',
    '1572804013309-59a88b7e92f1',  // sweater dress
    '1604336891291-58f3f29c43d4',
    '1485518882345-15568b007407'   // full outfit
  ],
  'formal-sets': [
    '1485518882345-15568b007407',
    '1581044777550-4cfa60707c03',  // covered outfit
    '1566174053879-31528523f8ae',  // boutique outfit
    '1485231183945-fffde7cc051e'   // shirt outfit
  ],
  'casual-sets': [
    '1572804013309-59a88b7e92f1',
    '1581044777550-4cfa60707c03',
    '1485518882345-15568b007407',
    '1566174053879-31528523f8ae'
  ],
  'pants': [
    '1542272604-787c3835535d',
    '1594633312681-425c7b97ccd1',
    '1473966968600-fa801b869a1a',
    '1551048632-24c2dec574a0'
  ],
  'shirts': [
    '1564584217132-2271feaeb3c5',  // white shirt
    '1485231183945-fffde7cc051e',  // linen shirt
    '1620799140408-edc6dcb6d633',  // silk blouse
    '1602810318383-e386cc2a3ccf'   // button shirt
  ],
  'trenchcoats': [
    '1591047139829-d91aecb6caea',  // trench
    '1539533018447-63fcce2678e3',  // long coat
    '1483721310020-03333e577078'   // overcoat
  ],
  'jackets': [
    '1551028719-00167b16eac5',     // jacket
    '1483721310020-03333e577078',
    '1591047139829-d91aecb6caea'
  ],
  'blouses': [
    '1564584217132-2271feaeb3c5',
    '1620799140408-edc6dcb6d633',
    '1485231183945-fffde7cc051e',
    '1593030761757-71fae45fa0e7'
  ],
  'skirts': [
    '1551803091-e20673f15770',
    '1583846783214-7229a91b20ed',
    '1485462537746-965f33f7f6a7'
  ]
};

function unsplash(id, w = 700) {
  // legacy fallback for any code path that still passes a raw Unsplash id
  return `https://images.unsplash.com/photo-${id}?auto=format&fit=crop&w=${w}&q=80`;
}

// Pick image — merges real R2 product images (window.RB_IMAGES) with the
// prototype's Unsplash pool so the gallery shows a mix of both. The (productIdx + offset)
// pair gives a deterministic but well-distributed selection across the merged pool.
function pickImg(catId, productIdx, offset /*, w = 700 */) {
  const realPool = (typeof window !== 'undefined' && window.RB_IMAGES && window.RB_IMAGES[catId]) || [];
  const unsplashPool = (POOL[catId] || POOL['casual-dresses']).map(unsplash);
  // Interleave: even slots → R2, odd slots → Unsplash (when both exist).
  const merged = [];
  const maxLen = Math.max(realPool.length, unsplashPool.length);
  for (let i = 0; i < maxLen; i++) {
    if (realPool[i]) merged.push(realPool[i]);
    if (unsplashPool[i]) merged.push(unsplashPool[i]);
  }
  if (!merged.length) return unsplash(unsplashPool[0]);
  return merged[(productIdx + offset) % merged.length];
}

const CATEGORIES = [
  { id:'formal-dresses', ar:'فساتين رسمية',  en:'Formal Dresses', tag:'Formal',  count:38, hue1:'#fadbe6', hue2:'#d46e96' },
  { id:'casual-dresses', ar:'فساتين عملية',  en:'Casual Dresses', tag:'Casual',  count:46, hue1:'#fde0ec', hue2:'#f79ab5' },
  { id:'formal-sets',    ar:'أطقم رسمية',    en:'Formal Sets',    tag:'Sets',    count:24, hue1:'#ead9c4', hue2:'#8c7456' },
  { id:'casual-sets',    ar:'أطقم عملية',    en:'Casual Sets',    tag:'Sets',    count:31, hue1:'#f5ead7', hue2:'#b89572' },
  { id:'pants',          ar:'بناطيل',        en:'Pants',          tag:'Bottoms', count:22, hue1:'#dfd6ca', hue2:'#7a7268' },
  { id:'shirts',         ar:'قمصان',         en:'Shirts',         tag:'Tops',    count:28, hue1:'#f3ebe0', hue2:'#c4a78a' },
  { id:'trenchcoats',    ar:'ترانش كوت',     en:'Trench Coats',   tag:'Outer',   count:14, hue1:'#d0c0a4', hue2:'#5a4a36' },
  { id:'jackets',        ar:'جاكيتات',       en:'Jackets',        tag:'Outer',   count:19, hue1:'#d8ccb6', hue2:'#7a6346' },
  { id:'blouses',        ar:'بلايز',         en:'Blouses',        tag:'Tops',    count:33, hue1:'#f8efe2', hue2:'#d8b89a' },
  { id:'skirts',         ar:'تنانير',         en:'Skirts',         tag:'Bottoms', count:21, hue1:'#e8e1d2', hue2:'#9c9588' }
];

const CAT_HEROES = {
  'formal-dresses': '1583846783214-7229a91b20ed',
  'casual-dresses': '1485462537746-965f33f7f6a7',
  'formal-sets':    '1485518882345-15568b007407',
  'casual-sets':    '1572804013309-59a88b7e92f1',
  'pants':          '1542272604-787c3835535d',
  'shirts':         '1564584217132-2271feaeb3c5',
  'trenchcoats':    '1591047139829-d91aecb6caea',
  'jackets':        '1551028719-00167b16eac5',
  'blouses':        '1485231183945-fffde7cc051e',
  'skirts':         '1551803091-e20673f15770'
};
function catImg(id) { return unsplash(CAT_HEROES[id], 800); }

// Each product can have a custom color palette — these map color → image variant
const COLOR_DEFS = {
  rose:    { ar:'وردي',   en:'Rose',   hex:'#f79ab5' },
  cream:   { ar:'كريمي',  en:'Cream',  hex:'#efe4d3' },
  mauve:   { ar:'موف',    en:'Mauve',  hex:'#a87a8e' },
  olive:   { ar:'زيتي',   en:'Olive',  hex:'#8c8c6a' },
  ink:     { ar:'أسود',   en:'Black',  hex:'#161214' },
  sand:    { ar:'رملي',   en:'Sand',   hex:'#cbb89b' },
  wine:    { ar:'نبيذي',  en:'Wine',   hex:'#7d2c3b' },
  sage:    { ar:'مريمية', en:'Sage',   hex:'#9bab92' },
  beige:   { ar:'بيج',    en:'Beige',  hex:'#d4bfa3' },
  blush:   { ar:'خوخي',   en:'Blush',  hex:'#e8b8c2' },
  charcoal:{ ar:'فحمي',   en:'Charcoal',hex:'#3c3539' },
  ivory:   { ar:'عاجي',   en:'Ivory',  hex:'#f3ece1' }
};

// Default color palette by category — different categories get different colors
const CAT_COLOR_SETS = {
  'formal-dresses': ['rose','wine','ink','blush'],
  'casual-dresses': ['rose','cream','sage','mauve'],
  'formal-sets':    ['ink','beige','wine','charcoal'],
  'casual-sets':    ['cream','sand','sage','rose'],
  'pants':          ['ink','beige','olive','charcoal'],
  'shirts':         ['ivory','cream','blush','sage'],
  'trenchcoats':    ['beige','sand','ink','charcoal'],
  'jackets':        ['ink','wine','beige','olive'],
  'blouses':        ['ivory','blush','cream','rose'],
  'skirts':         ['beige','ink','olive','sand']
};

const RAW_PRODUCTS = [
  ['p01','فستان "أوريانا" حرير',          '"Oriana" Silk Gown',         285, 340, 'formal-dresses', 'new'],
  ['p02','فستان "ياسمين" سهرة',            '"Yasmine" Evening Dress',    560, 640, 'formal-dresses', 'limited'],
  ['p03','فستان "صوفيا" دانتيل',            '"Sofia" Lace Dress',         410, null,'formal-dresses', null],
  ['p04','فستان "لارا" ساتان',              '"Lara" Satin Gown',          495, null,'formal-dresses', 'exclusive'],
  ['p05','فستان "مايا" قطن',                '"Maya" Cotton Dress',        215, null,'casual-dresses', null],
  ['p06','فستان "ليان" ميدي',               '"Layan" Midi Dress',         245, 285, 'casual-dresses', 'sale'],
  ['p07','فستان "هند" خفيف',                '"Hind" Linen Dress',         195, null,'casual-dresses', 'new'],
  ['p08','فستان "تالا" صيفي',               '"Tala" Summer Dress',        175, null,'casual-dresses', null],
  ['p09','طقم "نور" مزدوج',                  '"Nour" Two-Piece Suit',      520, null,'formal-sets',    'exclusive'],
  ['p10','طقم "روان" أناقة',                 '"Rawan" Elegance Set',       480, 550, 'formal-sets',    'sale'],
  ['p11','طقم "ميرا" بليزر',                 '"Mira" Blazer Set',          540, null,'formal-sets',    null],
  ['p12','طقم "سلمى" يومي',                  '"Salma" Everyday Set',       295, null,'casual-sets',    'best'],
  ['p13','طقم "زينة" قطن',                   '"Zeina" Cotton Set',         265, 310, 'casual-sets',    'sale'],
  ['p14','طقم "دانة" مريح',                  '"Dana" Comfort Set',         245, null,'casual-sets',    null],
  ['p15','بنطلون "ريما" واسع',                '"Rima" Wide-Leg Pants',      175, null,'pants',          'new'],
  ['p16','بنطلون "نادين" مستقيم',             '"Nadine" Straight Pants',    165, null,'pants',          null],
  ['p17','بنطلون "ميار" بليسيه',              '"Mayar" Pleated Pants',      195, 230, 'pants',          'sale'],
  ['p18','قميص "إيلا" حرير',                  '"Ella" Silk Shirt',          185, null,'shirts',         'exclusive'],
  ['p19','قميص "ليلى" أبيض',                  '"Layla" White Shirt',        155, null,'shirts',         null],
  ['p20','قميص "سارة" واسع',                  '"Sara" Oversized Shirt',     175, 195, 'shirts',         'sale'],
  ['p21','ترانش كوت "كلاسيك" بيج',             'Classic Beige Trench',       580, null,'trenchcoats',    'limited'],
  ['p22','ترانش كوت "بريق" طويل',              'Long Statement Trench',      620, 720, 'trenchcoats',    'sale'],
  ['p23','ترانش كوت "نور" قصير',                'Short "Nour" Trench',        495, null,'trenchcoats',    'new'],
  ['p24','جاكيت "أوبرا" مخمل',                  '"Opera" Velvet Jacket',      380, null,'jackets',        'exclusive'],
  ['p25','جاكيت "روز" قصير',                    '"Rose" Cropped Jacket',      295, 355, 'jackets',        'sale'],
  ['p26','جاكيت "إيلين" بليزر',                 '"Eileen" Blazer',            340, null,'jackets',        null],
  ['p27','بلوزة "ميلا" ساتان',                  '"Mila" Satin Blouse',        145, 175, 'blouses',        'sale'],
  ['p28','بلوزة "إيمي" دانتيل',                  '"Amy" Lace Blouse',          165, null,'blouses',        'new'],
  ['p29','بلوزة "كارمن" واسعة',                  '"Carmen" Flowy Blouse',      135, null,'blouses',        null],
  ['p30','تنورة "ريما" بليسيه',                  '"Rima" Pleated Skirt',       165, null,'skirts',         null],
  ['p31','تنورة "نور" ميدي',                      '"Nour" Midi Skirt',          145, 175, 'skirts',         'sale'],
  ['p32','تنورة "زهرة" طويلة',                    '"Zahra" Long Skirt',         185, null,'skirts',         'new']
];

const _catIdx = {};
const PRODUCTS = RAW_PRODUCTS.map((r) => {
  const cat = r[5];
  const i = (_catIdx[cat] ?? -1) + 1;
  _catIdx[cat] = i;

  // 4 image variants — one per color
  const imgs = [
    pickImg(cat, i, 0),
    pickImg(cat, i, 1),
    pickImg(cat, i, 2),
    pickImg(cat, i, 3)
  ];

  // Map this category's color set to image variants
  const colorIds = CAT_COLOR_SETS[cat] || ['rose','cream','ink','sand'];
  const colors = colorIds.map((cid, ix) => ({
    id: cid,
    ...COLOR_DEFS[cid],
    img: imgs[ix]
  }));

  return {
    id: r[0], ar: r[1], en: r[2], price: r[3], old: r[4], cat,
    badge: r[6],
    hue1: '#fde0ec', hue2: '#f79ab5',
    img:    { a: imgs[0], b: imgs[1] }, // for card hover
    images: imgs,                       // gallery
    colors                              // color → image map
  };
});

// Generic global color list (used in shop filters)
const COLOR_OPTS = Object.entries(COLOR_DEFS).map(([id, c]) => ({ id, ...c }));

const SIZE_OPTS = ['XS','S','M','L','XL'];

// Hero rotation — use 4 modest, elegant editorial photos
const HERO_IMGS = [
  unsplash('1583846783214-7229a91b20ed', 1600),  // long elegant dress
  unsplash('1485462537746-965f33f7f6a7', 1600),  // covered woman
  unsplash('1485518882345-15568b007407', 1600),  // outfit
  unsplash('1591047139829-d91aecb6caea', 1600)   // trench
];

window.RB = { CATEGORIES, PRODUCTS, COLOR_OPTS, SIZE_OPTS, HERO_IMGS, catImg, COLOR_DEFS };
