// data.jsx — loads products dynamically from /api/products
// SCENTS is populated after fetch; QUESTIONS and MATCHES stay static.

var SCENTS = {};

window.loadScents = function() {
  var productsReq = fetch('/api/products')
    .then(function(res) {
      if (!res.ok) throw new Error('Failed to load products');
      return res.json();
    })
    .then(function(list) {
      SCENTS = {};
      for (var i = 0; i < list.length; i++) {
        var p = list[i];
        SCENTS[p.scentId] = {
          id:          p.scentId,
          dbId:        p.id,
          name:        p.name,
          gender:      p.gender,
          tagline:     p.tagline,
          poem:        p.poem,
          description: p.description,
          match:       p.matchText,
          notes:       p.notes,
          accent:      p.accentColor,
          tint:        p.tintColor,
          deep:        p.deepColor,
          img:         p.imageUrl,
          img2:        p.image2Url,
          price:       p.price,
        };
      }
      window.SCENTS = SCENTS;
    });

  var matchesReq = fetch('/api/quiz/matches')
    .then(function(res) { if (!res.ok) return; return res.json(); })
    .then(function(data) { if (data) window.MATCHES = data; });

  var questionsReq = fetch('/api/quiz/questions')
    .then(function(res) { if (!res.ok) return; return res.json(); })
    .then(function(data) { if (data) window.QUESTIONS = data; });

  return Promise.all([productsReq, matchesReq, questionsReq]);
};

// Match logic
const MATCHES = {
  her: [
    { mood: 'Warm & sensual',     moment: 'Nights out & special occasions',   instinct: 'Something dark and complex',    match: 'desert-sky' },
    { mood: 'Soft & romantic',    moment: 'Everyday — work, errands, life',   instinct: 'Something light and airy',      match: 'cape-dusk' },
    { mood: 'Bold & magnetic',    moment: 'Wherever I go — it\'s always on',  instinct: 'Something warm and rich',       match: 'wild-coast' },
    { mood: 'Fresh & energising', moment: 'Everyday — work, errands, life',   instinct: 'Something clean and powerful',  match: 'summer-rain' },
    { mood: 'Mysterious & deep',  moment: 'Nights out & special occasions',   instinct: 'Something sweet and playful',   match: 'forest-mist' },
    { mood: 'Warm & sensual',     moment: 'Date nights & intimate moments',   instinct: 'Something sweet and playful',   match: 'golden-veld' },
  ],
  him: [
    { mood: 'Bold & magnetic',    moment: 'Nights out & special occasions',   instinct: 'Something dark and complex',    match: 'midnight-safari' },
    { mood: 'Fresh & energising', moment: 'Everyday — work, errands, life',   instinct: 'Something clean and powerful',  match: 'ocean-drift' },
    { mood: 'Mysterious & deep',  moment: 'Nights out & special occasions',   instinct: 'Something warm and rich',       match: 'iron-mountain' },
    { mood: 'Warm & sensual',     moment: 'Date nights & intimate moments',   instinct: 'Something sweet and playful',   match: 'storm-veld' },
    { mood: 'Bold & magnetic',    moment: 'Wherever I go — it\'s always on',  instinct: 'Something light and airy',      match: 'dust-horizon' },
  ],
};

const QUESTIONS = [
  {
    key: 'mood',
    title: 'What feeling do you want to carry with you?',
    label: '01 — Mood',
    options: ['Warm & sensual', 'Fresh & energising', 'Soft & romantic', 'Bold & magnetic', 'Mysterious & deep'],
  },
  {
    key: 'moment',
    title: 'When do you wear your signature scent most?',
    label: '02 — Moment',
    options: ['Everyday — work, errands, life', 'Nights out & special occasions', 'Date nights & intimate moments', 'Wherever I go — it\'s always on'],
  },
  {
    key: 'instinct',
    title: 'Which appeals to you most right now?',
    label: '03 — Instinct',
    options: ['Something light and airy', 'Something warm and rich', 'Something sweet and playful', 'Something dark and complex', 'Something clean and powerful'],
  },
];

function findMatch(gender, answers) {
  const table = (window.MATCHES || MATCHES)[gender] || [];
  const exact = table.find(r =>
    r.mood === answers.mood && r.moment === answers.moment && r.instinct === answers.instinct);
  if (exact) return SCENTS[exact.match];
  let best = null, bestScore = -1;
  table.forEach(r => {
    let s = 0;
    if (r.mood === answers.mood) s += 3;
    if (r.moment === answers.moment) s += 2;
    if (r.instinct === answers.instinct) s += 2;
    if (s > bestScore) { bestScore = s; best = r; }
  });
  return best ? SCENTS[best.match] : Object.values(SCENTS)[0];
}

Object.assign(window, { SCENTS, MATCHES, QUESTIONS, findMatch });
