import os
import csv
from pathlib import Path

# 定义路径
data_dir = Path(r'C:\Users\jerry\PyCharmProjects\footviz\footviz\data')
countries_dir = data_dir / 'countries'

# 联赛名映射到国家名
league_to_country = {
    "Premier League": "England",
    "EPL": "England",  # 在 matches.csv 中使用的缩写
    "Ligue 1": "France",
    "Bundesliga": "Germany",
    "La Liga": "Spain",
    "Chinese Super League": "China"
}

print("开始全面检查所有CSV文件...")

# 检查计数器
files_checked = 0
files_with_league_names = 0

# 遍历所有国家目录
for country_dir in countries_dir.iterdir():
    if country_dir.is_dir():
        country_name = country_dir.name
        print(f"\n检查国家: {country_name}")
        
        # 检查该国家目录下的 scores.csv 文件
        scores_file = country_dir / 'scores.csv'
        if scores_file.exists():
            files_checked += 1
            with open(scores_file, 'r', encoding='utf-8') as f:
                content = f.read()
            
            # 检查是否包含任何联赛名
            found_league = False
            for league_name in league_to_country.keys():
                if league_name in content:
                    print(f"  发现联赛名 '{league_name}' 在 {scores_file.name} 中")
                    found_league = True
                    files_with_league_names += 1
                    break
            
            if not found_league:
                print(f"  ✓ {scores_file.name} 中未发现联赛名")
        
        # 检查各球队目录
        for team_dir in country_dir.iterdir():
            if team_dir.is_dir():
                team_name = team_dir.name
                # print(f"  检查球队: {team_name}")
                
                # 检查 matches.csv 文件
                matches_file = team_dir / 'matches.csv'
                if matches_file.exists():
                    files_checked += 1
                    with open(matches_file, 'r', encoding='utf-8') as f:
                        content = f.read()
                    
                    # 检查是否包含任何联赛名
                    found_league = False
                    for league_name in league_to_country.keys():
                        if league_name in content:
                            print(f"  发现联赛名 '{league_name}' 在 {team_name}/matches.csv 中")
                            found_league = True
                            files_with_league_names += 1
                            break
                    
                    if not found_league:
                        print(f"  ✓ {team_name}/matches.csv 中未发现联赛名")
                
                # 检查球员文件
                players_dir = team_dir / 'players'
                if players_dir.exists():
                    for player_file in players_dir.iterdir():
                        if player_file.suffix == '.csv':
                            files_checked += 1
                            with open(player_file, 'r', encoding='utf-8') as f:
                                content = f.read()
                            
                            # 检查是否包含任何联赛名
                            found_league = False
                            for league_name in league_to_country.keys():
                                if league_name in content:
                                    print(f"  发现联赛名 '{league_name}' 在 {team_name}/players/{player_file.name} 中")
                                    found_league = True
                                    files_with_league_names += 1
                                    break
                            
                            if not found_league:
                                # print(f"  ✓ {team_name}/players/{player_file.name} 中未发现联赛名")
                                pass  # 不打印每条成功消息，避免输出过多

print(f"\n检查完成!")
print(f"总共检查了 {files_checked} 个文件")
print(f"发现 {files_with_league_names} 个文件中包含联赛名")
if files_with_league_names == 0:
    print("✓ 所有文件均已正确修改，未发现联赛名")
else:
    print("⚠ 仍有文件包含联赛名，请进一步处理")