import json
from pathlib import Path
from collections import defaultdict

BASE_DIR = Path(r"c:\Projects\KieuStory")
EP01_MUSE = BASE_DIR / "episodes/ep01/prompts/muse_prompts.json"
BANANA_PROMPTS = BASE_DIR / "02_AI_Prompts/gemini_banana_prompts.json"
EP01_SCREENPLAY = BASE_DIR / "episodes/ep01/screenplay.md"

with open(EP01_MUSE, "r", encoding="utf-8") as f:
    muse_data = json.load(f)
mp = muse_data.get("motion_prompts", {})

with open(BANANA_PROMPTS, "r", encoding="utf-8") as f:
    banana_data = json.load(f)

scenes = [f"ep01_scene{i:02d}" for i in range(1, 11)]

print(f"Total motion prompts in Ep01: {len(mp)}")
print("=" * 80)

for sc in scenes:
    shots = [k for k in mp.keys() if k.startswith(sc)]
    print(f"\n### {sc.upper()} (Total shots: {len(shots)})")
    
    # Analyze shots
    for i, sid in enumerate(shots):
        m_item = mp[sid]
        b_item = banana_data.get(sid, {})
        
        title = m_item.get("scene_title", "")
        cam = m_item.get("camera_motion", "")
        ref_sf = m_item.get("reference_start_frame", "")
        char_asset = m_item.get("character_asset_ref", "")
        b_anchor = b_item.get("character_anchor", "")
        b_env = b_item.get("environment_anchor", "")
        rev = m_item.get("reverse_motion", False)
        audio = m_item.get("audio_prompt", "")
        
        # Determine Cut type:
        # If shot 1 -> Cinematic Cut (establishing)
        # If character/subject changes -> Cinematic Cut
        # If continuous take -> Continuous Take
        cut_type = "Cinematic Cut" if (i == 0 or char_asset or "Tương ngộ" in title or "Đổi góc" in title) else "Continuous / Check"
        
        print(f"[{sid}]")
        print(f"  Title: {title}")
        print(f"  Camera: {cam}")
        print(f"  Char Asset: {char_asset if char_asset else 'None'}")
        print(f"  Banana Anchor: {b_anchor} | Env: {b_env}")
        print(f"  Reverse Motion: {rev}")
        print(f"  Ref Start Frame: {ref_sf[:80]}...")
