
class LevelCalculator {
  static const List<Map<String, dynamic>> _levelTable = [
    {'level': 1, 'requiredExp': 0, 'title': '見習い'},
    {'level': 2, 'requiredExp': 10, 'title': '歩き飲み'},
    {'level': 5, 'requiredExp': 50, 'title': '嗜み人'},
    {'level': 10, 'requiredExp': 100, 'title': '呑兵衛'},
    {'level': 20, 'requiredExp': 200, 'title': '酒豪'},
    {'level': 30, 'requiredExp': 300, 'title': '利き酒師'},
    {'level': 50, 'requiredExp': 500, 'title': '日本酒伝道師'},
    {'level': 100, 'requiredExp': 1000, 'title': 'ポンシュマスター'},
  ];

  static int getLevel(int totalExp) {
    for (var i = _levelTable.length - 1; i >= 0; i--) {
      if (totalExp >= (_levelTable[i]['requiredExp'] as int)) {
        return _levelTable[i]['level'] as int;
      }
    }
    return 1; // Fallback
  }

  static String getTitle(int totalExp) {
    final level = getLevel(totalExp);
    return _levelTable.firstWhere(
      (entry) => entry['level'] == level,
      orElse: () => _levelTable[0],
    )['title'] as String;
  }

  static double getProgress(int totalExp) {
    final currentLevel = getLevel(totalExp);
    final currentEntry = _levelTable.firstWhere((e) => e['level'] == currentLevel);
    
    // Find next level entry
    final currentIndex = _levelTable.indexOf(currentEntry);
    if (currentIndex == _levelTable.length - 1) return 1.0; // Max level
    
    final nextEntry = _levelTable[currentIndex + 1];
    
    final currentLevelExp = currentEntry['requiredExp'] as int;
    final nextLevelExp = nextEntry['requiredExp'] as int;
    
    if (nextLevelExp == currentLevelExp) return 1.0;
    
    return (totalExp - currentLevelExp) / (nextLevelExp - currentLevelExp);
  }
  
  static int getExpToNextLevel(int totalExp) {
     final currentLevel = getLevel(totalExp);
     final currentEntry = _levelTable.firstWhere((e) => e['level'] == currentLevel);
     
     // Find next level entry
    final currentIndex = _levelTable.indexOf(currentEntry);
    if (currentIndex == _levelTable.length - 1) return 0; // Max level
    
    final nextEntry = _levelTable[currentIndex + 1];
    return (nextEntry['requiredExp'] as int) - totalExp;
  }
}
