import json
import unittest
import sys
import os
from pathlib import Path

sys.path.insert(0, os.getcwd())

# Load existing muse_ai_video_prompts.json
existing_path = Path("02_AI_Prompts/muse_ai_video_prompts.json")
with open(existing_path, "r", encoding="utf-8") as f:
    existing_data = json.load(f)

# Load our 188 shots
new_shots_path = Path(".agents/teamwork/spec_miner_m2_3/ep01_muse_prompts_188_shots.json")
with open(new_shots_path, "r", encoding="utf-8") as f:
    new_shots = json.load(f)

# Create merged data in memory
merged_data = json.loads(json.dumps(existing_data))
motion = merged_data.setdefault("motion_prompts", {})

# Keep non-ep01 keys, update/replace all ep01_scene keys
for k, v in new_shots.items():
    motion[k] = v

print(f"Total merged motion prompts: {len(motion)}")
ep01_shots = [k for k in motion if "ep01" in k.lower()]
print(f"Total EP01 motion prompts: {len(ep01_shots)}")

# Now mock load_muse_prompts_data in tests
import tests
tests.load_muse_prompts_data = lambda: merged_data

import tests.test_tier1_features
tests.test_tier1_features.load_muse_prompts_data = lambda: merged_data

from tests.test_tier1_features import TestTier1Features

suite = unittest.TestSuite()
# Add F5, F6, F7 tests
suite.addTest(TestTier1Features("test_f5_04_directional_motion_vector_guard"))
suite.addTest(TestTier1Features("test_f5_05_reverse_physics_prohibition"))
suite.addTest(TestTier1Features("test_f6_01_lip_sync_guard_present_in_offscreen_dialogue"))
suite.addTest(TestTier1Features("test_f6_02_scene08_kim_trong_hairpin_guard"))
suite.addTest(TestTier1Features("test_f6_03_voiceover_prompts_lip_guard"))
suite.addTest(TestTier1Features("test_f6_04_lip_sync_bilingual_directive"))
suite.addTest(TestTier1Features("test_f6_05_on_screen_vs_off_screen_classification"))
suite.addTest(TestTier1Features("test_f7_01_muse_prompt_registry_ep01_shot_count"))
suite.addTest(TestTier1Features("test_f7_03_shot_id_format_compliance"))
suite.addTest(TestTier1Features("test_f7_04_audio_guard_clause_in_all_prompts"))
suite.addTest(TestTier1Features("test_f7_05_prompt_non_emptiness_and_length"))

runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
assert result.wasSuccessful(), "Some tests failed!"
print("\n>>> ALL TEST_TIER1_FEATURES FOR F5, F6, F7 PASSED WITH ZERO FAILURES! <<<")
