"""
修复episodes.json数据问题
"""
import json
from pathlib import Path


def fix_episodes_data():
    """修复episodes.json中的问题数据"""
    episodes_file = Path(__file__).parent.parent / "data" / "episodes.json"

    if not episodes_file.exists():
        print(f"episodes.json not found at {episodes_file}")
        return

    with open(episodes_file, 'r', encoding='utf-8') as f:
        data = json.load(f)

    fixed_count = 0

    for episode_id, episode in data.items():
        modified = False

        # 修复EP1的标题
        if episode.get("number") == 1 and episode.get("title") == "11":
            episode["title"] = "苏园初会"
            modified = True
            fixed_count += 1

        # 修复EP2的错误内容
        if episode.get("number") == 2 and "内容创作失败" in str(episode.get("content", "")):
            episode["content"] = ""
            episode["status"] = "pending"
            modified = True
            fixed_count += 1

        # 确保所有pending剧集有正确的结构
        if episode.get("status") == "pending":
            if not episode.get("outline"):
                episode["outline"] = None
            if not episode.get("content"):
                episode["content"] = ""
            if not episode.get("structure"):
                episode["structure"] = {
                    "episodeNumber": episode.get("number"),
                    "scenes": [],
                    "keyEvents": []
                }
            modified = True
            fixed_count += 1

        # 修复qualityScore为None的completed剧集
        if episode.get("status") == "completed" and episode.get("qualityScore") is None:
            episode["qualityScore"] = 0.0
            modified = True
            fixed_count += 1

    # 保存修复后的数据
    with open(episodes_file, 'w', encoding='utf-8') as f:
        json.dump(data, f, ensure_ascii=False, indent=2)

    print(f"修复完成！共修复 {fixed_count} 个问题")
    return True


if __name__ == "__main__":
    fix_episodes_data()
