import os
import csv
import re
from pathlib import Path

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

# 国家和联赛映射
league_mapping = {
    "Premier League": "England",
    "Ligue 1": "France",
    "Bundesliga": "Germany",
    "La Liga": "Spain",
    "Chinese Super League": "China"
}

# 反向映射
country_mapping = {v: k for k, v in league_mapping.items()}

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()
            
            # 检查是否包含联赛名
            league_name = country_mapping.get(country_name, "")
            if league_name and league_name in content:
                print(f"  发现联赛名 '{league_name}' 在 {scores_file.name} 中")
                # 替换为国家名
                new_content = content.replace(league_name, country_name)
                # 写回文件
                with open(scores_file, 'w', encoding='utf-8') as f:
                    f.write(new_content)
                print(f"  已将 '{league_name}' 替换为 '{country_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():
                    # 读取文件内容
                    with open(matches_file, 'r', encoding='utf-8') as f:
                        content = f.read()
                    
                    # 检查是否包含联赛名
                    league_name = country_mapping.get(country_name, "")
                    if league_name and league_name in content:
                        print(f"    发现联赛名 '{league_name}' 在 {matches_file.name} 中")
                        # 替换为国家名
                        new_content = content.replace(league_name, country_name)
                        # 写回文件
                        with open(matches_file, 'w', encoding='utf-8') as f:
                            f.write(new_content)
                        print(f"    已将 '{league_name}' 替换为 '{country_name}'")
                
                # 检查球员文件
                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()
                            
                            # 检查是否包含联赛名
                            league_name = country_mapping.get(country_name, "")
                            if league_name and league_name in content:
                                print(f"    发现联赛名 '{league_name}' 在 {player_file.name} 中")
                                # 替换为国家名
                                new_content = content.replace(league_name, country_name)
                                # 写回文件
                                with open(player_file, 'w', encoding='utf-8') as f:
                                    f.write(new_content)
                                print(f"    已将 '{league_name}' 替换为 '{country_name}'")

print("\n检查和修正完成!")