// Move Classification Engine
// Classifies moves as: brilliant, great, best, good, book, inaccuracy, mistake, blunder
import { emoji } from '../../../core/theme.js';
import { t } from '../../../core/i18n.js';

const THRESHOLDS = {
  blunder: 2.0,     // Lost 2+ pawns worth of eval
  mistake: 1.0,     // Lost 1-2 pawns
  inaccuracy: 0.5,  // Lost 0.5-1 pawn
  good: 0.2,        // Within 0.2 of best
  great: 0.05,      // Within 0.05 of best (basically best in complex position)
  brilliant: -0.5   // Made a move that looks bad but is actually winning (sacrifice)
};

export function classifyMove(evalBefore, evalAfter, bestEval, isPlayerWhite, moveNum) {
  // All evals from white's perspective
  const sign = isPlayerWhite ? 1 : -1;
  const evalChange = (evalAfter - evalBefore) * sign;
  const lossFromBest = (bestEval - evalAfter) * sign;

  // Book moves (first 2 full moves = 4 half-moves)
  if (moveNum <= 2) return { class: 'book', symbol: emoji('book', '📖', 14), color: 'var(--text-secondary)' };

  // If eval is mate-level, special handling
  if (Math.abs(evalAfter) > 900) {
    if (evalAfter * sign > 0) return { class: 'great', symbol: '!', color: 'var(--blue)' };
    return { class: 'blunder', symbol: '??', color: 'var(--error)' };
  }

  if (lossFromBest >= THRESHOLDS.blunder) {
    return { class: 'blunder', symbol: '??', color: 'var(--error)' };
  }
  if (lossFromBest >= THRESHOLDS.mistake) {
    return { class: 'mistake', symbol: '?', color: 'var(--orange)' };
  }
  if (lossFromBest >= THRESHOLDS.inaccuracy) {
    return { class: 'inaccuracy', symbol: '?!', color: 'var(--amber)' };
  }
  if (lossFromBest <= THRESHOLDS.good) {
    // Check for brilliant (sacrifice that maintains or improves eval)
    if (evalChange < -1 && lossFromBest <= 0.1) {
      return { class: 'brilliant', symbol: '!!', color: 'var(--cyan)' };
    }
    if (lossFromBest <= THRESHOLDS.great) {
      return { class: 'best', symbol: emoji('best_move_star', '★', 14), color: 'var(--emerald)' };
    }
    return { class: 'great', symbol: '!', color: 'var(--blue)' };
  }

  return { class: 'good', symbol: '', color: 'var(--text-secondary)' };
}

export function calculateAccuracy(classifications) {
  if (!classifications || classifications.length === 0) return 0;

  const weights = { brilliant: 100, great: 97, best: 100, good: 85, book: 90, inaccuracy: 60, mistake: 30, blunder: 0 };
  let total = 0;
  let count = 0;

  for (const c of classifications) {
    if (c.class === 'book') continue; // Skip book moves
    total += weights[c.class] || 50;
    count++;
  }

  return count > 0 ? Math.round(total / count) : 0;
}

export function getAccuracyLabel(accuracy) {
  if (accuracy >= 95) return { label: t('classifier.excellent'), labelEn: 'Excellent', color: 'var(--emerald)' };
  if (accuracy >= 85) return { label: t('classifier.great'), labelEn: 'Great', color: 'var(--blue)' };
  if (accuracy >= 70) return { label: t('classifier.good'), labelEn: 'Good', color: 'var(--amber)' };
  if (accuracy >= 50) return { label: t('classifier.average'), labelEn: 'Average', color: 'var(--orange)' };
  return { label: t('classifier.poor'), labelEn: 'Poor', color: 'var(--error)' };
}

export function getMoveClassSummary(classifications) {
  const summary = { brilliant: 0, great: 0, best: 0, good: 0, book: 0, inaccuracy: 0, mistake: 0, blunder: 0 };
  for (const c of classifications) {
    summary[c.class] = (summary[c.class] || 0) + 1;
  }
  return summary;
}
