import json
import os
from pathlib import Path

BASE_DIR = Path(r"c:\Projects\KieuStory")
with open(BASE_DIR / "episodes/ep01/prompts/muse_prompts.json", "r", encoding="utf-8") as f:
    muse_data = json.load(f)
mp = muse_data.get("motion_prompts", {})

with open(BASE_DIR / "02_AI_Prompts/gemini_banana_prompts.json", "r", encoding="utf-8") as f:
    banana_data = json.load(f)

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

# Helper to classify shot type
def classify_shot(sid, prev_sid, shot_data, prev_shot_data):
    # Rule 1: First shot of scene is always Cinematic Cut
    if prev_sid is None:
        return "Cinematic Cut (Scene Opener)"
    
    title = shot_data.get("scene_title", "").lower()
    prev_title = prev_shot_data.get("scene_title", "").lower() if prev_shot_data else ""
    cam = shot_data.get("camera_motion", "").lower()
    
    # Check if character changes
    asset = shot_data.get("character_asset_ref", "")
    prev_asset = prev_shot_data.get("character_asset_ref", "") if prev_shot_data else ""
    
    # Check subjects
    chars = ["kim trọng", "thúy kiều", "thúy vân", "vương quan", "vương ông", "vương bà", "đạm tiên", "cụ ông", "đứa cháu"]
    curr_char = next((c for c in chars if c in title), "")
    prev_char = next((c for c in chars if c in prev_title), "")
    
    if curr_char and prev_char and curr_char != prev_char:
        return f"Cinematic Cut (Character Switch: {prev_char.title()} -> {curr_char.title()})"
    
    if "toàn cảnh" in title and ("cận cảnh" in prev_title or "đặc tả" in prev_title):
        return "Cinematic Cut (Reverse Angle / Extreme Scale Jump)"
    
    if "chuyển cảnh" in title or "đổi góc" in title or "time-warp" in title:
        return "Cinematic Cut (Camera Repositioning)"
        
    return "Continuous Take (Chained Tail Frame)"

print("Classifier ready. Testing...")
for sc in scenes[:3]:
    shots = [k for k in mp.keys() if k.startswith(sc)]
    prev_s = None
    for s in shots[:4]:
        stype = classify_shot(s, prev_s, mp[s], mp.get(prev_s))
        print(f"  {s}: {stype}")
        prev_s = s
