export const calculateExercise = (exercise) => {
  let sum = 0;
  let count = 0;

  if (exercise.subA && exercise.subB) {
    [...exercise.subA, ...exercise.subB].forEach((val) => {
      const num = parseFloat(val);
      if (!isNaN(num)) {
        sum += num;
        count++;
      }
    });
  } else {
    exercise.values.forEach((val) => {
      const num = parseFloat(val);
      if (!isNaN(num)) {
        sum += num;
        count++;
      }
    });
  }

  const durchschnitt = count > 0 ? sum / count : 0;
  const points = durchschnitt * exercise.faktor;

  return { durchschnitt, points };
};

export const calculateTotal = (exercises = []) =>
  exercises.reduce((sum, ex) => sum + (ex.points || 0), 0);

export const getPatTypeColor = (patType) => {
  const colors = {
    'PAT Start': 'bg-green-100 text-green-800',
    'PAT 1': 'bg-blue-100 text-blue-800',
    'PAT 2': 'bg-purple-100 text-purple-800',
    'PAT 3': 'bg-orange-100 text-orange-800'
  };
  return colors[patType] || 'bg-gray-100 text-gray-800';
};

export const getAchievement = (patType, points) => {
  const achievements = {
    'PAT Start': [
      { min: 1000, color: 'bg-yellow-400', text: 'text-black', name: '2. Schwarz-Weiss Sport WPA', subtitle: 'Freizeit-Spieler' },
      { min: 800, color: 'bg-white', text: 'text-black', name: '1. Weiss', subtitle: '' },
      { min: 600, color: 'bg-gray-200', text: 'text-gray-800', name: 'PAT Start Teilnahme', subtitle: '' },
      { min: 0, color: 'bg-gray-100', text: 'text-gray-500', name: 'Nicht bestanden', subtitle: '' }
    ],
    'PAT 1': [
      { min: 1000, color: 'bg-green-700', text: 'text-white', name: '4. Dunkelgrün', subtitle: 'ca. Bezirksliga' },
      { min: 800, color: 'bg-green-300', text: 'text-black', name: '3. Hellgrün', subtitle: 'ca. Kreisliga' },
      { min: 600, color: 'bg-gray-200', text: 'text-gray-800', name: 'PAT 1 Teilnahme', subtitle: '' },
      { min: 0, color: 'bg-gray-100', text: 'text-gray-500', name: 'Nicht bestanden', subtitle: '' }
    ],
    'PAT 2': [
      { min: 1000, color: 'bg-blue-700', text: 'text-white', name: '6. Dunkelblau', subtitle: 'ca. Verbandsliga' },
      { min: 800, color: 'bg-blue-300', text: 'text-black', name: '5. Hellblau', subtitle: 'ca. Landesliga' },
      { min: 600, color: 'bg-gray-200', text: 'text-gray-800', name: 'PAT 2 Teilnahme', subtitle: '' },
      { min: 0, color: 'bg-gray-100', text: 'text-gray-500', name: 'Nicht bestanden', subtitle: '' }
    ],
    'PAT 3': [
      { min: 1000, color: 'bg-pink-400', text: 'text-white', name: '10. Pink', subtitle: 'Ein Weltstar!' },
      { min: 850, color: 'bg-yellow-400', text: 'text-black', name: '9. Gold', subtitle: 'internationaler Topsportler' },
      { min: 700, color: 'bg-red-600', text: 'text-white', name: '8. Rot', subtitle: 'ca. 1. Bundesliga' },
      { min: 550, color: 'bg-black', text: 'text-white', name: '7. Schwarz', subtitle: 'ca. 2. Bundesliga' },
      { min: 0, color: 'bg-gray-100', text: 'text-gray-500', name: 'Nicht bestanden', subtitle: '' }
    ]
  };

  const patAchievements = achievements[patType] || achievements['PAT Start'];
  return patAchievements.find((a) => points >= a.min) || patAchievements[patAchievements.length - 1];
};
