/* global */
// content.jsx — channel data: daily topic rotation, weekly themes, growth plan.

// Days since channel launch — used to compute today's episode number and the
// 30-topic rotation. The launch date is the channel's birthday.
const LAUNCH_DATE = new Date('2026-01-01T00:00:00');

function daysSince(date = new Date()) {
  const ms = date.getTime() - LAUNCH_DATE.getTime();
  return Math.max(0, Math.floor(ms / 86400000));
}

// ── Weekly themes ───────────────────────────────────────────────────────────
const WEEKLY_THEMES = [
  // 0 = Sunday (JS getDay)
  { day: 'Sunday',    name: 'Singalong Sunday',   tag: 'Best-of mix',       color: '#FF6FB0', emoji: '🎤' },
  { day: 'Monday',    name: 'Animal Adventures',  tag: 'Farm & zoo songs',  color: '#5DCB6E', emoji: '🐮' },
  { day: 'Tuesday',   name: 'Counting & ABCs',    tag: 'Learn-along',       color: '#4FB8FF', emoji: '🔤' },
  { day: 'Wednesday', name: 'Bedtime Lullabies',  tag: 'Calm-down songs',   color: '#5470B8', emoji: '🌙' },
  { day: 'Thursday',  name: 'Action Songs',       tag: 'Clap, stomp, move', color: '#FFD43D', emoji: '👏' },
  { day: 'Friday',    name: 'Family Favorites',   tag: 'Classic rhymes',    color: '#FF5252', emoji: '🎵' },
  { day: 'Saturday',  name: 'Story Songs',        tag: 'Long-form rhymes',  color: '#E8A100', emoji: '📖' },
];

function themeForDay(date = new Date()) {
  return WEEKLY_THEMES[date.getDay()];
}

// ── Daily headline topics — 30 distinct rotating headliners ────────────────
// Each topic anchors today's episode with a featured "headliner" song id and
// a curated playlist drawn from songs.jsx.
const DAILY_TOPICS = [
  { id: 1,  topic: 'Stars & Sleepy Time',     featured: 'twinkle',   hook: 'Sing to the stars 🌟',         playlist: ['twinkle','rain','row'] },
  { id: 2,  topic: 'Down on the Farm',         featured: 'macdonald', hook: 'E-I-E-I-OOOH!',                playlist: ['macdonald','mary','baabaa'] },
  { id: 3,  topic: 'Best Friends Forever',     featured: 'mary',      hook: 'Mary & her little lamb',       playlist: ['mary','baabaa','happy'] },
  { id: 4,  topic: 'Rainy Day, Sunny Day',     featured: 'itsy',      hook: 'Climb climb climb!',           playlist: ['itsy','rain','row'] },
  { id: 5,  topic: 'Splash & Float',           featured: 'row',       hook: 'Row row row your boat',        playlist: ['row','fiveducks','itsy'] },
  { id: 6,  topic: 'Quack Quack Quack',        featured: 'fiveducks', hook: '5 little ducks 🦆',             playlist: ['fiveducks','macdonald','row'] },
  { id: 7,  topic: 'Tick Tock Time',           featured: 'hickory',   hook: 'The mouse and the clock',      playlist: ['hickory','abc','twinkle'] },
  { id: 8,  topic: 'Rain Rain Go Away',        featured: 'rain',      hook: 'Bring the sun back!',          playlist: ['rain','itsy','happy'] },
  { id: 9,  topic: 'Wheels & Wonder',          featured: 'bus',       hook: 'Round and round!',             playlist: ['bus','happy','abc'] },
  { id: 10, topic: 'Happy Hands & Feet',       featured: 'happy',     hook: 'Clap, stomp, shout!',          playlist: ['happy','bus','abc'] },
  { id: 11, topic: 'My First ABCs',            featured: 'abc',       hook: 'Sing your letters',            playlist: ['abc','hickory','happy'] },
  { id: 12, topic: 'Lullaby Lights Out',       featured: 'twinkle',   hook: 'Calm-down playlist',           playlist: ['twinkle','row','mary'] },
  { id: 13, topic: 'Baa Baa Black Sheep',      featured: 'baabaa',    hook: 'Three bags full!',             playlist: ['baabaa','mary','macdonald'] },
  { id: 14, topic: 'Moo Cow Morning',          featured: 'macdonald', hook: 'Wake up the farm 🐄',           playlist: ['macdonald','baabaa','fiveducks'] },
  { id: 15, topic: 'Itsy Bitsy Garden',        featured: 'itsy',      hook: 'Climb the water spout',        playlist: ['itsy','rain','happy'] },
  { id: 16, topic: 'Counting 1-2-3',           featured: 'fiveducks', hook: '5 - 4 - 3 - 2 - 1!',           playlist: ['fiveducks','abc','hickory'] },
  { id: 17, topic: 'Big Red Bus',              featured: 'bus',       hook: 'All through the town',         playlist: ['bus','happy','macdonald'] },
  { id: 18, topic: 'Mouse Squeak Squeak',      featured: 'hickory',   hook: 'Tiny feet, big song',          playlist: ['hickory','baabaa','abc'] },
  { id: 19, topic: 'Stomp & Clap',             featured: 'happy',     hook: 'Action-songs blast',           playlist: ['happy','bus','macdonald'] },
  { id: 20, topic: 'Sleepy Sheep Counting',    featured: 'baabaa',    hook: 'One, two, three sheep…',       playlist: ['baabaa','twinkle','mary'] },
  { id: 21, topic: 'Pond Friends',             featured: 'row',       hook: 'Ducks, boats & dreams',        playlist: ['row','fiveducks','twinkle'] },
  { id: 22, topic: 'Daytime Animals',          featured: 'macdonald', hook: 'Sun-up sing-along',            playlist: ['macdonald','baabaa','itsy'] },
  { id: 23, topic: 'Spider in the Sun',        featured: 'itsy',      hook: 'After the rain comes…',        playlist: ['itsy','happy','abc'] },
  { id: 24, topic: 'Lamb & Friends',           featured: 'mary',      hook: 'Follow Mary to school',        playlist: ['mary','baabaa','happy'] },
  { id: 25, topic: 'Rumble & Splash',          featured: 'rain',      hook: 'Storm-day singalong',          playlist: ['rain','itsy','row'] },
  { id: 26, topic: 'Wheels Go Round',          featured: 'bus',       hook: 'Beep beep beep!',              playlist: ['bus','macdonald','happy'] },
  { id: 27, topic: 'Counting Sheep at Night',  featured: 'baabaa',    hook: 'Drift off slow',               playlist: ['baabaa','twinkle','row'] },
  { id: 28, topic: 'Hop & Sing',               featured: 'happy',     hook: 'Wiggle-it-out fun',            playlist: ['happy','abc','bus'] },
  { id: 29, topic: 'ABC Storytime',            featured: 'abc',       hook: 'Letters & friends',            playlist: ['abc','hickory','mary'] },
  { id: 30, topic: 'Twinkle Forever',          featured: 'twinkle',   hook: 'Star-bright bedtime',          playlist: ['twinkle','row','baabaa'] },
];

function todayTopic(date = new Date()) {
  const idx = daysSince(date) % DAILY_TOPICS.length;
  return { ...DAILY_TOPICS[idx], date, dayIndex: daysSince(date) };
}

function topicForDay(dayOffset, baseDate = new Date()) {
  const d = new Date(baseDate);
  d.setDate(d.getDate() + dayOffset);
  const idx = daysSince(d) % DAILY_TOPICS.length;
  return {
    ...DAILY_TOPICS[idx],
    date: d,
    dayIndex: daysSince(d),
    theme: themeForDay(d),
  };
}

// ── Channel growth plan — 1M subs in 12 months ─────────────────────────────
// Targets follow a believable kidvid curve: slow start, hockey-stick after
// month 4 once the algorithm catches a few hits.
const GROWTH_PLAN = [
  { week: 0,  target: 0,        label: 'Launch' },
  { week: 1,  target: 100,      label: 'First viewers' },
  { week: 2,  target: 500 },
  { week: 4,  target: 2400,     label: '1 month' },
  { week: 8,  target: 11000,    label: 'First viral video' },
  { week: 12, target: 32000,    label: '3 months' },
  { week: 16, target: 78000,    label: 'Algorithm push' },
  { week: 20, target: 165000 },
  { week: 24, target: 305000,   label: '6 months' },
  { week: 28, target: 475000 },
  { week: 32, target: 640000,   label: 'Press coverage' },
  { week: 36, target: 780000,   label: '9 months' },
  { week: 40, target: 880000 },
  { week: 44, target: 945000 },
  { week: 48, target: 985000 },
  { week: 52, target: 1000000,  label: '🎉 1M SUBS' },
];

function currentSubs(date = new Date()) {
  const weeks = daysSince(date) / 7;
  // linear interpolate between plan points
  for (let i = 0; i < GROWTH_PLAN.length - 1; i++) {
    const a = GROWTH_PLAN[i], b = GROWTH_PLAN[i + 1];
    if (weeks >= a.week && weeks < b.week) {
      const p = (weeks - a.week) / (b.week - a.week);
      return Math.round(a.target + (b.target - a.target) * p);
    }
  }
  return GROWTH_PLAN[GROWTH_PLAN.length - 1].target;
}

function nextMilestone(date = new Date()) {
  const subs = currentSubs(date);
  const labeled = GROWTH_PLAN.filter(p => p.label && p.target > subs);
  return labeled[0] || GROWTH_PLAN[GROWTH_PLAN.length - 1];
}

// ── Catalogue — last 8 episodes as YouTube-style cards ──────────────────────
function recentEpisodes(date = new Date(), count = 8) {
  const out = [];
  for (let i = 0; i < count; i++) {
    const topic = topicForDay(-i, date);
    const baseViews = i === 0 ? 12400 : Math.round(85000 + Math.random() * 250000 + (count - i) * 30000);
    out.push({
      ...topic,
      offsetDays: -i,
      views: i === 0 ? '12K watching' : `${(baseViews / 1000).toFixed(0)}K views`,
      ageDays: i,
      duration: '10:47',
      isLive: i === 0,
      isNew: i < 2,
    });
  }
  return out;
}

function upcomingEpisodes(date = new Date(), count = 7) {
  return Array.from({ length: count }).map((_, i) => topicForDay(i + 1, date));
}

// Format helpers
function fmtSubs(n) {
  if (n >= 1_000_000) return (n / 1_000_000).toFixed(2) + 'M';
  if (n >= 10_000) return (n / 1_000).toFixed(0) + 'K';
  if (n >= 1_000) return (n / 1_000).toFixed(1) + 'K';
  return String(n);
}
function fmtDate(d) {
  return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' });
}
function fmtWeekday(d) {
  return d.toLocaleDateString(undefined, { weekday: 'short' });
}

Object.assign(window, {
  LAUNCH_DATE, WEEKLY_THEMES, DAILY_TOPICS, GROWTH_PLAN,
  daysSince, themeForDay, todayTopic, topicForDay,
  currentSubs, nextMilestone, recentEpisodes, upcomingEpisodes,
  fmtSubs, fmtDate, fmtWeekday,
});
