// Opening Explorer — shows popular moves and win rates for positions
// Uses a simplified database of common positions

const EXPLORER_DATA = {
  'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1': [
    { move: 'e4', games: 5000, white: 54, draw: 26, black: 20 },
    { move: 'd4', games: 4200, white: 55, draw: 28, black: 17 },
    { move: 'Nf3', games: 2100, white: 52, draw: 30, black: 18 },
    { move: 'c4', games: 1800, white: 53, draw: 29, black: 18 },
    { move: 'f4', games: 400, white: 48, draw: 22, black: 30 },
  ],
  'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1': [
    { move: 'e5', games: 2200, white: 52, draw: 28, black: 20 },
    { move: 'c5', games: 2800, white: 52, draw: 25, black: 23 },
    { move: 'e6', games: 1200, white: 54, draw: 28, black: 18 },
    { move: 'c6', games: 1000, white: 53, draw: 30, black: 17 },
    { move: 'd5', games: 600, white: 56, draw: 24, black: 20 },
    { move: 'g6', games: 500, white: 55, draw: 25, black: 20 },
  ],
  'rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2': [
    { move: 'Nf3', games: 1800, white: 54, draw: 28, black: 18 },
    { move: 'f4', games: 400, white: 50, draw: 22, black: 28 },
    { move: 'Bc4', games: 300, white: 53, draw: 26, black: 21 },
    { move: 'Nc3', games: 250, white: 52, draw: 30, black: 18 },
    { move: 'd4', games: 200, white: 54, draw: 24, black: 22 },
  ],
  'rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2': [
    { move: 'Nc6', games: 1400, white: 53, draw: 28, black: 19 },
    { move: 'Nf6', games: 500, white: 55, draw: 26, black: 19 },
    { move: 'd6', games: 200, white: 54, draw: 28, black: 18 },
  ],
  'r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3': [
    { move: 'Bb5', games: 800, white: 54, draw: 28, black: 18 },
    { move: 'Bc4', games: 500, white: 53, draw: 27, black: 20 },
    { move: 'd4', games: 300, white: 55, draw: 24, black: 21 },
    { move: 'Nc3', games: 200, white: 52, draw: 30, black: 18 },
  ],
  'rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2': [
    { move: 'Nf3', games: 2000, white: 53, draw: 26, black: 21 },
    { move: 'Nc3', games: 600, white: 52, draw: 28, black: 20 },
    { move: 'c3', games: 400, white: 54, draw: 25, black: 21 },
    { move: 'd4', games: 300, white: 51, draw: 24, black: 25 },
  ],
  'rnbqkbnr/pppp1ppp/4p3/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2': [
    { move: 'd4', games: 1000, white: 55, draw: 28, black: 17 },
    { move: 'd3', games: 200, white: 51, draw: 30, black: 19 },
    { move: 'Nf3', games: 150, white: 52, draw: 29, black: 19 },
  ],
  'rnbqkbnr/pp1ppppp/2p5/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2': [
    { move: 'd4', games: 800, white: 54, draw: 30, black: 16 },
    { move: 'Nf3', games: 200, white: 52, draw: 28, black: 20 },
    { move: 'Nc3', games: 150, white: 53, draw: 27, black: 20 },
  ],
  'rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq - 0 1': [
    { move: 'd5', games: 2000, white: 54, draw: 29, black: 17 },
    { move: 'Nf6', games: 2500, white: 54, draw: 28, black: 18 },
    { move: 'f5', games: 300, white: 56, draw: 22, black: 22 },
    { move: 'e6', games: 200, white: 53, draw: 30, black: 17 },
  ],
};

export function lookup(fen) {
  // Normalize FEN (strip move counters for matching)
  const normalized = fen.split(' ').slice(0, 4).join(' ');

  // Try exact match first
  if (EXPLORER_DATA[fen]) return EXPLORER_DATA[fen];

  // Try normalized match
  for (const [key, data] of Object.entries(EXPLORER_DATA)) {
    if (key.split(' ').slice(0, 4).join(' ') === normalized) return data;
  }

  return null;
}

export function formatExplorerEntry(entry) {
  const total = entry.games;
  return {
    move: entry.move,
    games: total,
    whiteWin: entry.white,
    draw: entry.draw,
    blackWin: entry.black,
    bar: { w: entry.white, d: entry.draw, b: entry.black }
  };
}

export function renderExplorerBar(white, draw, black) {
  return `<div style="display:flex;height:6px;border-radius:3px;overflow:hidden;width:80px;">
    <div style="width:${white}%;background:var(--text-primary);"></div>
    <div style="width:${draw}%;background:var(--text-secondary);"></div>
    <div style="width:${black}%;background:#1e293b;"></div>
  </div>`;
}
