import json
import sys

sys.stdout.reconfigure(encoding='utf-8')

with open('.agents/teamwork/spec_miner_m2_2/gemini_banana_ep01_registry.json', 'r', encoding='utf-8') as f:
    registry = json.load(f)

with open('.agents/teamwork/spec_miner_m2_2/extracted_shots.json', 'r', encoding='utf-8') as f:
    shots = json.load(f)

print(f"Total registry entries: {len(registry)}")
print(f"Total screenplay shots: {len(shots)}")

assert len(registry) == 188, f"Expected 188 entries, got {len(registry)}"
assert len(shots) == 188, f"Expected 188 screenplay shots, got {len(shots)}"

errors = []
expected_keys = {"character_anchor", "environment_anchor", "prompt", "resolution"}

for s in shots:
    shot_id = s['shot_id']
    if shot_id not in registry:
        errors.append(f"Missing shot {shot_id}")
        continue
    entry = registry[shot_id]
    
    # Check schema keys
    if set(entry.keys()) != expected_keys:
        errors.append(f"Shot {shot_id} has invalid keys: {set(entry.keys())}")
    
    # Check resolution
    if entry.get("resolution") != "1280x720":
        errors.append(f"Shot {shot_id} resolution is not 1280x720: {entry.get('resolution')}")
    
    # Check non-empty
    if not entry.get("character_anchor"):
        errors.append(f"Shot {shot_id} empty character_anchor")
    if not entry.get("environment_anchor"):
        errors.append(f"Shot {shot_id} empty environment_anchor")
    if not entry.get("prompt"):
        errors.append(f"Shot {shot_id} empty prompt")
        
    prompt = entry.get("prompt", "")
    # Check negative prompt presence
    if "--no " not in prompt or "--ar 16:9" not in prompt:
        errors.append(f"Shot {shot_id} prompt missing negative constraints or aspect ratio")

if errors:
    print(f"FAILED with {len(errors)} errors:")
    for e in errors[:10]:
        print(f"  - {e}")
    sys.exit(1)
else:
    print("ALL 188 SHOTS PASSED VALIDATION! Schema, anchors, resolution, and constraints 100% compliant.")
