// data.jsx — Sample data + visual helpers
//
// ┌──────────────────────────────────────────────────────────────────────┐
// │ MOCK-DATA LAYER                                                      │
// │ ----------------------------------------------------------------     │
// │ SAMPLE_ITEMS is only used when the user is signed-out (locked demo). │
// │ Signed-in flows fetch from Firestore via window.VHFirebase and       │
// │ never touch this list.                                               │
// │ All names/codes/studios here are invented placeholders.              │
// └──────────────────────────────────────────────────────────────────────┘

// Deterministic hash → hue for cover gradients
function hashHue(s) {
  let h = 0;
  for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) >>> 0;
  return h % 360;
}

// Gradient cover keyed off the item code — abstract, no imagery
function coverGradient(code) {
  const h = hashHue(code);
  const h2 = (h + 38) % 360;
  return `radial-gradient(120% 90% at 18% 14%, oklch(0.46 0.18 ${h}) 0%, oklch(0.26 0.10 ${h2}) 55%, oklch(0.14 0.05 ${h2}) 100%)`;
}

// Two-letter "redacted" label when privacy is locked
function redactCode(code) {
  return code.replace(/[A-Z0-9]/g, '•');
}

// Sample library (sparse — 6 items)
const SAMPLE_ITEMS = [
  {
    id: '1',
    code: 'SVR-241',
    title: 'Last Train From Shinagawa',
    actress: ['Hana Mori'],
    studio: 'Northstar Pictures',
    series: 'Late Night Vol. 4',
    releaseDate: '2024-09-12',
    tags: ['drama', 'rain'],
    rating: 5,
    status: 'favorite',
    note: 'Re-watch in autumn — best with headphones.',
    reminderAt: null,
    addedAt: '2026-05-20',
    watchedAt: '2026-05-18',
  },
  {
    id: '2',
    code: 'AKT-088',
    title: 'Quiet Hours',
    actress: ['Rin Sato', 'Hana Mori'],
    studio: 'Akari Films',
    series: null,
    releaseDate: '2024-11-03',
    tags: ['slow', 'duo'],
    rating: 4,
    status: 'watched',
    note: '',
    reminderAt: null,
    addedAt: '2026-05-16',
    watchedAt: '2026-05-16',
  },
  {
    id: '3',
    code: 'MOON-012',
    title: 'Blue Hour Drift',
    actress: ['Yui Nakamura'],
    studio: 'Vanta Studio',
    series: 'Drift',
    releaseDate: '2025-02-21',
    tags: ['neon', 'urban'],
    rating: 4,
    status: 'want_to_watch',
    note: 'Saved from a friend.',
    reminderAt: '2026-06-01',
    addedAt: '2026-05-12',
    watchedAt: null,
  },
  {
    id: '4',
    code: 'LMN-307',
    title: 'After the Festival',
    actress: ['Rin Sato'],
    studio: 'Lumen Pictures',
    series: null,
    releaseDate: '2025-04-08',
    tags: ['summer'],
    rating: 3,
    status: 'rewatch',
    note: '',
    reminderAt: null,
    addedAt: '2026-05-09',
    watchedAt: '2026-04-20',
  },
  {
    id: '5',
    code: 'SVR-118',
    title: 'Paper Lanterns',
    actress: ['Hana Mori'],
    studio: 'Northstar Pictures',
    series: 'Late Night Vol. 2',
    releaseDate: '2023-10-30',
    tags: ['drama', 'autumn'],
    rating: 5,
    status: 'favorite',
    note: '',
    reminderAt: null,
    addedAt: '2026-05-02',
    watchedAt: '2026-04-12',
  },
  {
    id: '6',
    code: 'KIRA-044',
    title: 'Glass Mornings',
    actress: ['Yui Nakamura'],
    studio: 'Kira Works',
    series: null,
    releaseDate: '2025-06-14',
    tags: ['quiet'],
    rating: 0,
    status: 'want_to_watch',
    note: '',
    reminderAt: null,
    addedAt: '2026-04-28',
    watchedAt: null,
  },
];

// Status presentation
const STATUS_META = {
  want_to_watch: { label: 'Want',     short: 'Want',     dot: '#9aa3b2' },
  watched:       { label: 'Watched',  short: 'Watched',  dot: '#22c55e' },
  favorite:      { label: 'Favorite', short: 'Fav',      dot: '#eab308' },
  rewatch:       { label: 'Rewatch',  short: 'Rewatch',  dot: '#60a5fa' },
  skip:          { label: 'Skip',     short: 'Skip',     dot: '#6b7280' },
};

// Backward-compat normalizer:
// - tolerates `actress` (old/sample) AND `actresses` (new)
// - derives `castIds` from name keys when missing (so legacy videos still link)
function normalizeItem(it) {
  if (!it) return it;
  const actresses = it.actresses || it.actress || [];
  const tags = it.tags || [];
  const castIds = it.castIds || (window.VHCast
    ? actresses.map(n => 'derived:' + window.VHCast.normalizeCastName(n))
    : []);
  const sourceBadges = [];
  const sourceDomain = it.sourceDomain || extractSourceDomain(it.sourceUrl);
  if (sourceDomain) sourceBadges.push(sourceDomain);
  if (it.missavUrl) sourceBadges.push('MissAV');
  return { ...it, actresses, tags, castIds, sourceDomain, sourceBadges };
}

function extractSourceDomain(sourceUrl) {
  try { return sourceUrl ? new URL(sourceUrl).hostname : ''; }
  catch { return ''; }
}

Object.assign(window, {
  SAMPLE_ITEMS, STATUS_META, hashHue, coverGradient, redactCode, normalizeItem,
});
