import os
import shutil
from pathlib import Path

# 定义路径
data_dir = Path(r'C:\Users\jerry\PyCharmProjects\footviz\footviz\data')
matches_dir = data_dir / 'matches'
team_data_dir = data_dir / 'team_data'
players_dir = data_dir / 'players' 
new_structure_dir = data_dir / 'countries'

# 确保新结构目录存在
new_structure_dir.mkdir(exist_ok=True)

# 国家/地区映射
countries = {
    'france': 'France',
    'germany': 'Germany',
    'spain': 'Spain',
    'uk': 'England'
}

# 1. 重新组织比赛数据
print("重新组织比赛数据...")
for country_code, country_name in countries.items():
    country_path = new_structure_dir / country_name
    country_path.mkdir(exist_ok=True)
    
    # 获取原始比赛数据路径
    original_matches_path = matches_dir / country_code
    
    if original_matches_path.exists():
        # 遍历该国家的所有球队
        for team_file in original_matches_path.iterdir():
            if team_file.suffix == '.csv':
                # 获取球队名称（去除.csv扩展名）
                team_name = team_file.stem
                
                # 创建球队目录
                team_dir = country_path / team_name
                team_dir.mkdir(exist_ok=True)
                
                # 创建matches.csv文件
                new_match_file = team_dir / 'matches.csv'
                shutil.copy(team_file, new_match_file)
                print(f"复制 {team_file.name} 到 {country_name}/{team_name}/matches.csv")

# 2. 重新组织球员数据
print("\n重新组织球员数据...")
# 球员数据已按球队分组在 players/teams 目录中
players_teams_dir = players_dir / 'teams'

if players_teams_dir.exists():
    # 遍历每个球队目录
    for team_dir in players_teams_dir.iterdir():
        if team_dir.is_dir():
            team_name = team_dir.name
            
            # 查找该球队属于哪个国家
            country_name = None
            for country in countries.values():
                country_path = new_structure_dir / country
                if (country_path / team_name).exists():
                    country_name = country
                    break
            
            # 如果找到了对应的国家
            if country_name:
                # 在该国家的球队目录下创建players子目录
                target_team_dir = new_structure_dir / country_name / team_name
                players_subdir = target_team_dir / 'players'
                players_subdir.mkdir(exist_ok=True)
                
                # 复制所有球员文件到新位置
                for player_file in team_dir.iterdir():
                    if player_file.suffix == '.csv':
                        shutil.copy(player_file, players_subdir / player_file.name)
                        print(f"复制 {player_file.name} 到 {country_name}/{team_name}/players/")
            else:
                print(f"警告: 未找到球队 {team_name} 对应的国家")

print("\n目录重组完成!")
print("\n新的目录结构:")
print("countries/")
for country_name in countries.values():
    country_path = new_structure_dir / country_name
    if country_path.exists():
        print(f"  {country_name}/")
        for team_dir in country_path.iterdir():
            if team_dir.is_dir():
                print(f"    {team_dir.name}/")
                print(f"      matches.csv")
                players_dir = team_dir / 'players'
                if players_dir.exists() and any(players_dir.iterdir()):
                    print(f"      players/")
                    # 只显示前几个球员文件示例
                    player_files = list(players_dir.iterdir())
                    for player_file in player_files[:3]:
                        if player_file.suffix == '.csv':
                            print(f"        {player_file.name}")
                    if len(player_files) > 3:
                        print(f"        ... and {len(player_files) - 3} more")

print("\n注意: 原始数据文件仍然保留在原位置，如需清理请手动删除。")