/**
 * Loads lesson content. Prefers AI-generated content (data/lessons.generated.json,
 * written by `npm run seed`) and falls back to the hand-written starter set in
 * data/lessons.json. Server-only.
 */
import fs from 'node:fs';
import path from 'node:path';
import type { Lesson } from './types';
import fallback from '../data/lessons.json';

type LessonMap = Record<string, Lesson[]>;

let _cache: LessonMap | null = null;

function loadGenerated(): LessonMap | null {
  try {
    const p = path.join(process.cwd(), 'data', 'lessons.generated.json');
    if (!fs.existsSync(p)) return null;
    const raw = fs.readFileSync(p, 'utf-8');
    return JSON.parse(raw) as LessonMap;
  } catch {
    return null;
  }
}

function getMap(): LessonMap {
  if (_cache) return _cache;
  const generated = loadGenerated();
  // Merge: generated wins per-subject, fallback fills the gaps.
  _cache = { ...(fallback as unknown as LessonMap), ...(generated ?? {}) };
  return _cache;
}

export function getLessonsForSubject(subjectId: string): Lesson[] {
  return getMap()[subjectId] ?? [];
}

export function getLesson(subjectId: string, lessonId?: string): Lesson | undefined {
  const lessons = getLessonsForSubject(subjectId);
  if (lessons.length === 0) return undefined;
  if (!lessonId) return lessons[0];
  return lessons.find((l) => l.id === lessonId) ?? lessons[0];
}

/* ------------------ Alphabet drag-and-drop match game ------------------ */

export interface MatchPair {
  id: string;
  letter: string; // draggable
  word: string; // drop target label
  emoji: string;
}

export const ALPHABET_MATCH: MatchPair[] = [
  { id: 'm-a', letter: 'আ', word: 'আম', emoji: '🥭' },
  { id: 'm-b', letter: 'ব', word: 'বই', emoji: '📕' },
  { id: 'm-f', letter: 'ফ', word: 'ফুল', emoji: '🌼' },
  { id: 'm-m', letter: 'ম', word: 'মাছ', emoji: '🐟' },
];

/* ---------------------- "শেখো" phase (teach before quiz) ---------------------- */

export interface LearnCard {
  id: string;
  big: string; // large focus (letter / number / picture)
  sub: string; // short label under it
  emoji: string; // picture
  tip: string; // one-line, child-friendly explanation
}

const LEARN: Record<string, LearnCard[]> = {
  alphabet: [
    { id: 'l-a', big: 'আ', sub: 'আম', emoji: '🥭', tip: '‘আ’ দিয়ে শুরু হয় আম।' },
    { id: 'l-k', big: 'ক', sub: 'কলা', emoji: '🍌', tip: '‘ক’ দিয়ে শুরু হয় কলা।' },
    { id: 'l-b', big: 'ব', sub: 'বই', emoji: '📕', tip: '‘ব’ দিয়ে শুরু হয় বই।' },
    { id: 'l-f', big: 'ফ', sub: 'ফুল', emoji: '🌼', tip: '‘ফ’ দিয়ে শুরু হয় ফুল।' },
    { id: 'l-m', big: 'ম', sub: 'মাছ', emoji: '🐟', tip: '‘ম’ দিয়ে শুরু হয় মাছ।' },
  ],
  math: [
    { id: 'l-count', big: '১ ২ ৩', sub: 'গুনতে শিখি', emoji: '🔢', tip: 'আঙুল দিয়ে গুনি: এক, দুই, তিন।' },
    { id: 'l-add', big: '২ + ৩', sub: '= ৫', emoji: '➕', tip: '২টার সাথে ৩টা যোগ করলে ৫টা হয়।' },
    { id: 'l-sub', big: '৭ − ২', sub: '= ৫', emoji: '➖', tip: '৭ থেকে ২টা সরালে ৫টা থাকে।' },
    { id: 'l-cmp', big: '৯ > ৫', sub: 'বড় সংখ্যা', emoji: '📏', tip: '৯ হলো ৫-এর চেয়ে বড়।' },
  ],
  science: [
    { id: 'l-sun', big: '☀️', sub: 'সূর্য', emoji: '🌤️', tip: 'দিনের বেলা আকাশে সূর্য থাকে।' },
    { id: 'l-root', big: '🌱', sub: 'শিকড়', emoji: '🌳', tip: 'গাছের শিকড় মাটির নিচে থাকে।' },
    { id: 'l-ice', big: '💧', sub: 'পানি', emoji: '🧊', tip: 'বরফ গলে পানি হয়ে যায়।' },
    { id: 'l-bird', big: '🐦', sub: 'পাখি', emoji: '🕊️', tip: 'পাখির ডানা আছে, তাই উড়তে পারে।' },
  ],
  story: [
    { id: 'l-turtle', big: '🐢', sub: 'কচ্ছপ', emoji: '🏁', tip: 'ধীরে হলেও থেমে না গিয়ে কচ্ছপ জেতে।' },
    { id: 'l-word', big: 'সূ-র্য', sub: '২ অক্ষর', emoji: '🔤', tip: '‘সূর্য’ শব্দে দুইটা অক্ষর আছে।' },
    { id: 'l-good', big: '👍', sub: 'ভালো কাজ', emoji: '🎁', tip: 'ভালো কাজ করলে ভালো ফল পাওয়া যায়।' },
    { id: 'l-read', big: '📖', sub: 'গল্প পড়া', emoji: '✨', tip: 'গল্প পড়লে কল্পনাশক্তি বাড়ে।' },
  ],
};

export function getLearnCards(subjectId: string): LearnCard[] {
  return LEARN[subjectId] ?? [];
}
