// Move Classification Engine
// Classifies moves as: brilliant, great, best, good, book, inaccuracy, mistake, blunder
import { emoji } from '../../../core/theme.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: '#94a3b8' };

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

  if (lossFromBest >= THRESHOLDS.blunder) {
    return { class: 'blunder', symbol: '??', color: '#EF4444' };
  }
  if (lossFromBest >= THRESHOLDS.mistake) {
    return { class: 'mistake', symbol: '?', color: '#F97316' };
  }
  if (lossFromBest >= THRESHOLDS.inaccuracy) {
    return { class: 'inaccuracy', symbol: '?!', color: '#FBBF24' };
  }
  if (lossFromBest <= THRESHOLDS.good) {
    // Check for brilliant (sacrifice that maintains or improves eval)
    if (evalChange < -1 && lossFromBest <= 0.1) {
      return { class: 'brilliant', symbol: '!!', color: '#06B6D4' };
    }
    if (lossFromBest <= THRESHOLDS.great) {
      return { class: 'best', symbol: emoji('best_move_star', '★', 14), color: '#10B981' };
    }
    return { class: 'great', symbol: '!', color: '#3B82F6' };
  }

  return { class: 'good', symbol: '', color: '#94a3b8' };
}

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: 'ممتاز', labelEn: 'Excellent', color: '#10B981' };
  if (accuracy >= 85) return { label: 'جيد جداً', labelEn: 'Great', color: '#3B82F6' };
  if (accuracy >= 70) return { label: 'جيد', labelEn: 'Good', color: '#FBBF24' };
  if (accuracy >= 50) return { label: 'متوسط', labelEn: 'Average', color: '#F97316' };
  return { label: 'ضعيف', labelEn: 'Poor', color: '#EF4444' };
}

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;
}
