// ECO Opening Database — maps move sequences to opening names
const OPENINGS = {
  '1.e4': { name: "King's Pawn Opening", eco: 'B00' },
  '1.e4 e5': { name: "Open Game", eco: 'C20' },
  '1.e4 e5 2.Nf3': { name: "King's Knight Opening", eco: 'C40' },
  '1.e4 e5 2.Nf3 Nc6': { name: "Four Knights Game", eco: 'C44' },
  '1.e4 e5 2.Nf3 Nc6 3.Bb5': { name: "Ruy Lopez", nameAr: "الإسبانية", eco: 'C60' },
  '1.e4 e5 2.Nf3 Nc6 3.Bc4': { name: "Italian Game", nameAr: "الإيطالية", eco: 'C50' },
  '1.e4 e5 2.Nf3 Nc6 3.d4': { name: "Scotch Game", nameAr: "الاسكتلندية", eco: 'C45' },
  '1.e4 e5 2.Nf3 Nf6': { name: "Petrov's Defense", nameAr: "دفاع بتروف", eco: 'C42' },
  '1.e4 e5 2.f4': { name: "King's Gambit", nameAr: "غامبيت الملك", eco: 'C30' },
  '1.e4 c5': { name: "Sicilian Defense", nameAr: "الصقلية", eco: 'B20' },
  '1.e4 c5 2.Nf3 d6': { name: "Sicilian: Najdorf", nameAr: "الصقلية: نايدورف", eco: 'B90' },
  '1.e4 c5 2.Nf3 Nc6': { name: "Sicilian: Classical", nameAr: "الصقلية: الكلاسيكية", eco: 'B30' },
  '1.e4 c5 2.Nf3 e6': { name: "Sicilian: French", nameAr: "الصقلية: الفرنسية", eco: 'B40' },
  '1.e4 e6': { name: "French Defense", nameAr: "الدفاع الفرنسي", eco: 'C00' },
  '1.e4 e6 2.d4 d5': { name: "French: Classical", nameAr: "الفرنسي: الكلاسيكي", eco: 'C01' },
  '1.e4 c6': { name: "Caro-Kann Defense", nameAr: "كارو-كان", eco: 'B10' },
  '1.e4 c6 2.d4 d5': { name: "Caro-Kann: Main Line", nameAr: "كارو-كان: الخط الرئيسي", eco: 'B12' },
  '1.e4 d5': { name: "Scandinavian Defense", nameAr: "الإسكندنافي", eco: 'B01' },
  '1.e4 d6': { name: "Pirc Defense", nameAr: "دفاع بيرك", eco: 'B07' },
  '1.e4 g6': { name: "Modern Defense", nameAr: "الدفاع الحديث", eco: 'B06' },
  '1.e4 Nf6': { name: "Alekhine's Defense", nameAr: "دفاع أليخين", eco: 'B02' },
  '1.d4': { name: "Queen's Pawn Opening", eco: 'D00' },
  '1.d4 d5': { name: "Closed Game", eco: 'D00' },
  '1.d4 d5 2.c4': { name: "Queen's Gambit", nameAr: "غامبيت الملكة", eco: 'D06' },
  '1.d4 d5 2.c4 e6': { name: "QGD: Classical", nameAr: "غامبيت الملكة المرفوض", eco: 'D30' },
  '1.d4 d5 2.c4 dxc4': { name: "QGA: Accepted", nameAr: "غامبيت الملكة المقبول", eco: 'D20' },
  '1.d4 d5 2.c4 c6': { name: "Slav Defense", nameAr: "الدفاع السلافي", eco: 'D10' },
  '1.d4 Nf6': { name: "Indian Defense", eco: 'A45' },
  '1.d4 Nf6 2.c4 g6': { name: "King's Indian Defense", nameAr: "هندي الملك", eco: 'E60' },
  '1.d4 Nf6 2.c4 e6': { name: "Nimzo/Queen's Indian", nameAr: "نيمزو-هندية", eco: 'E00' },
  '1.d4 Nf6 2.c4 e6 3.Nc3 Bb4': { name: "Nimzo-Indian Defense", nameAr: "نيمزو-هندية", eco: 'E20' },
  '1.d4 Nf6 2.c4 c5': { name: "Benoni Defense", nameAr: "دفاع بنوني", eco: 'A56' },
  '1.d4 f5': { name: "Dutch Defense", nameAr: "الدفاع الهولندي", eco: 'A80' },
  '1.c4': { name: "English Opening", nameAr: "الإنجليزية", eco: 'A10' },
  '1.Nf3': { name: "Réti Opening", nameAr: "ريتي", eco: 'A04' },
  '1.f4': { name: "Bird's Opening", nameAr: "افتتاحية بيرد", eco: 'A02' },
  '1.b3': { name: "Larsen's Opening", nameAr: "لارسن", eco: 'A01' },
  '1.g3': { name: "Hungarian Opening", nameAr: "الهنغارية", eco: 'A00' },
};

export function identify(moveHistory) {
  if (!moveHistory || moveHistory.length === 0) return null;

  let bestMatch = null;
  let bestLength = 0;

  // Build move string progressively and find longest match
  let moveStr = '';
  for (let i = 0; i < Math.min(moveHistory.length, 10); i++) {
    const move = moveHistory[i];
    const san = move.san || move;
    const moveNum = Math.floor(i / 2) + 1;
    if (i % 2 === 0) {
      moveStr += (moveStr ? ' ' : '') + moveNum + '.' + san;
    } else {
      moveStr += ' ' + san;
    }

    if (OPENINGS[moveStr]) {
      bestMatch = OPENINGS[moveStr];
      bestLength = i + 1;
    }
  }

  return bestMatch;
}

export function getOpeningName(moveHistory, lang = 'ar') {
  const opening = identify(moveHistory);
  if (!opening) return null;
  return lang === 'ar' ? (opening.nameAr || opening.name) : opening.name;
}

export function getEco(moveHistory) {
  const opening = identify(moveHistory);
  return opening?.eco || null;
}
