import json
from pathlib import Path

ep01_prompts_file = Path("episodes/ep01/prompts/muse_prompts.json")
master_prompts_file = Path("02_AI_Prompts/muse_ai_video_prompts.json")

data = json.loads(ep01_prompts_file.read_text(encoding="utf-8"))
motion_prompts = data.get("motion_prompts", {})

# 1. Update Shot 11 with Muse's recommended Over-the-shoulder angle
shot11 = motion_prompts.get("ep01_scene05_shot11", {})
shot11["camera_motion"] = "Góc máy qua vai từ phía sau Thúy Kiều nhìn về phía nấm mồ hoang (Over-the-shoulder Shot from behind Thúy Kiều)."
shot11["motion_prompt"] = (
    "Tao video 10 giay dien anh co phong tiep noi muot ma: Góc máy qua vai từ phía sau Thúy Kiều nhìn về phía nấm mồ hoang (Over-the-shoulder Shot from behind Thúy Kiều).. "
    "Dáng hình Thúy Kiều thanh mảnh đứng lặng gió chiều, tà áo lụa xanh biếc lay động khẽ khàng, trâm ngọc cài tóc rung rinh thanh thoát. "
    "Tầm nhìn qua bờ vai nàng hướng thẳng về nấm mồ sè sè đơn độc giữa triền cỏ khô lạnh lẽo. Nàng đứng lặng với nỗi ngỡ ngàng xót xa trước cảnh tượng hương khói vắng tanh giữa tiết Thanh minh.. "
    "Chuyển động vi mô chân thực, chuyển động tự nhiên thuận chiều, tuyệt đối không giật cục hay chuyển động ngược chiều vật lý. "
    "Thần thái Thúy Kiều thanh nhã, u sầu trước cảnh ngộ người xưa. "
    "Âm thanh: Tiếng trâm ngọc khẽ va nhẹ tạo âm thanh thanh thoát, tiếng gió chiều lay lay dải lụa xanh trên vai áo Kiều, tiếng cỏ khô xào xạc. "
    "Quy tắc âm thanh: Tuyệt đối KHÔNG sinh nhạc nền (no music/BGM), không âm thanh điện tử, không tạp âm rè nhiễu. Chỉ sinh âm thanh môi trường tự nhiên (foley, ambience) và thoại nhân vật chân thực."
)

# 2. Sanitize any minor ages and extreme crying triggers across scene 05
for k, v in motion_prompts.items():
    if "ep01_scene05" in k:
        prompt = v.get("motion_prompt", "")
        # Replace age mentions that trigger minor filters
        prompt = prompt.replace("cô gái 16 tuổi", "thiếu nữ thanh xuân")
        prompt = prompt.replace("chàng trai 15 tuổi", "thiếu niên khôi ngô")
        prompt = prompt.replace("16 tuổi", "thanh xuân")
        prompt = prompt.replace("15 tuổi", "thiếu niên")
        # Soften extreme crying to cinematic restrained sorrow
        prompt = prompt.replace("khóc nức nở", "lệ đọng khóe mi, ánh mắt nghẹn ngào xót xa")
        v["motion_prompt"] = prompt

ep01_prompts_file.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"Updated {ep01_prompts_file}")

# Sync to master if exists
if master_prompts_file.exists():
    master_data = json.loads(master_prompts_file.read_text(encoding="utf-8"))
    if "motion_prompts" not in master_data:
        master_data["motion_prompts"] = {}
    for k, v in motion_prompts.items():
        if "ep01_scene05" in k:
            master_data["motion_prompts"][k] = v
    master_prompts_file.write_text(json.dumps(master_data, indent=2, ensure_ascii=False), encoding="utf-8")
    print(f"Synced to {master_prompts_file}")

print("Prompt refinement and safety sanitization complete.")
