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文件...")

# 遍历所有国家目录
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():
            with open(scores_file, 'r', encoding='utf-8') as f:
                content = f.read()
            
            # 替换任何联赛名为国家名
            modified = False
            for league_name, cntry_name in league_to_country.items():
                if league_name in content:
                    content = content.replace(league_name, cntry_name)
                    print(f"  在 {scores_file.name} 中将 '{league_name}' 替换为 '{cntry_name}'")
                    modified = True
            
            if modified:
                with open(scores_file, 'w', encoding='utf-8') as f:
                    f.write(content)
        
        # 检查各球队目录
        for team_dir in country_dir.iterdir():
            if team_dir.is_dir():
                team_name = team_dir.name
                
                # 检查 matches.csv 文件
                matches_file = team_dir / 'matches.csv'
                if matches_file.exists():
                    with open(matches_file, 'r', encoding='utf-8') as f:
                        content = f.read()
                    
                    # 替换任何联赛名为国家名
                    modified = False
                    for league_name, cntry_name in league_to_country.items():
                        if league_name in content:
                            content = content.replace(league_name, cntry_name)
                            print(f"  在 {team_name}/matches.csv 中将 '{league_name}' 替换为 '{cntry_name}'")
                            modified = True
                    
                    if modified:
                        with open(matches_file, 'w', encoding='utf-8') as f:
                            f.write(content)
                
                # 检查球员文件
                players_dir = team_dir / 'players'
                if players_dir.exists():
                    for player_file in players_dir.iterdir():
                        if player_file.suffix == '.csv':
                            with open(player_file, 'r', encoding='utf-8') as f:
                                content = f.read()
                            
                            # 替换任何联赛名为国家名
                            modified = False
                            for league_name, cntry_name in league_to_country.items():
                                if league_name in content:
                                    content = content.replace(league_name, cntry_name)
                                    print(f"  在 {team_name}/players/{player_file.name} 中将 '{league_name}' 替换为 '{cntry_name}'")
                                    modified = True
                            
                            if modified:
                                with open(player_file, 'w', encoding='utf-8') as f:
                                    f.write(content)

print("\n修正完成!")