import { t } from '../../../core/i18n.js';
import { emoji } from '../../../core/theme.js';

// Rank tier system
export const TIERS = [
  { get name() { return t('rank.bronze'); }, nameEn: 'Bronze', min: 0, max: 999, color: '#CD7F32', get icon() { return emoji('medal_bronze', '🥉', 16); } },
  { get name() { return t('rank.silver'); }, nameEn: 'Silver', min: 1000, max: 1199, color: '#C0C0C0', get icon() { return emoji('medal_silver', '🥈', 16); } },
  { get name() { return t('rank.gold'); }, nameEn: 'Gold', min: 1200, max: 1399, color: 'var(--gold-soft)', get icon() { return emoji('medal_gold', '🥇', 16); } },
  { get name() { return t('rank.platinum'); }, nameEn: 'Platinum', min: 1400, max: 1599, color: '#00CED1', get icon() { return emoji('gem', '💎', 16); } },
  { get name() { return t('rank.diamond'); }, nameEn: 'Diamond', min: 1600, max: 1799, color: '#B9F2FF', get icon() { return emoji('diamond', '💠', 16); } },
  { get name() { return t('rank.master'); }, nameEn: 'Master', min: 1800, max: 2000, color: '#FF4500', get icon() { return emoji('crown', '👑', 16); } },
  { get name() { return t('rank.grandmaster'); }, nameEn: 'Grandmaster', min: 2000, max: 9999, color: '#8B008B', get icon() { return emoji('medal_gold', '🏅', 16); } },
];

export function getTier(rating) {
  for (let i = TIERS.length - 1; i >= 0; i--) {
    if (rating >= TIERS[i].min) return TIERS[i];
  }
  return TIERS[0];
}

export function getTierProgress(rating) {
  const tier = getTier(rating);
  const range = tier.max - tier.min;
  if (range <= 0) return 1;
  return Math.min(1, (rating - tier.min) / range);
}

export function getNextTier(rating) {
  const current = getTier(rating);
  const idx = TIERS.indexOf(current);
  return idx < TIERS.length - 1 ? TIERS[idx + 1] : null;
}
