#!/usr/bin/env python3
"""
Image Comparison Tool for Broken Spire Anime Opening
Creates side-by-side comparisons of generated frames vs reference images
"""

import os
from pathlib import Path

# Mapping of generated frames to reference images
COMPARISONS = {
    "02_ash_birth_00002_": ["Ash.png", "Ash 2.png"],
    "03_far_future_ash_evil_00002_": ["Far-Future Ash.png"],
    "05_everly_soldier_00002_": ["Everly.png"],
    "06_eva_doctor_00002_": ["Éva Moreau.png"],
    "07_nova_warrior_00002_": ["Nova Human.png", "Nova devil.png"],
    "08_violet_devil_00002_": ["Violet humaine.png", "Violet Devil.png"],
    "09_lin_weishan_00002_": ["Lin Weishan.png"],
    "10_tc23_esper_00002_": ["TC-23.png", "Esper.png"],
    "11_jonas_ghost_00002_": ["Jonas.png"],
}

GENERATED_DIR = "/mnt/c/Users/fbmor/broken-spire-comparison/generated"
REFERENCES_DIR = "/mnt/c/Users/fbmor/broken-spire-comparison/references"

print("=" * 60)
print("BROKEN SPIRE - CHARACTER CONSISTENCY CHECK")
print("=" * 60)

# List all generated files
print("\n📁 GENERATED FRAMES:")
generated_files = sorted(os.listdir(GENERATED_DIR))
for f in generated_files:
    print(f"  - {f}")

print("\n📁 REFERENCE IMAGES:")
ref_files = sorted(os.listdir(REFERENCES_DIR))
for f in ref_files:
    print(f"  - {f}")

print("\n" + "=" * 60)
print("CHARACTER CONSISTENCY VALIDATION")
print("=" * 60)

for frame, refs in COMPARISONS.items():
    print(f"\n🔍 {frame}")
    print(f"   Expected references: {', '.join(refs)}")
    
    # Check if generated file exists
    gen_path = os.path.join(GENERATED_DIR, f"{frame}.png")
    if os.path.exists(gen_path):
        print("   ✅ Generated: EXISTS")
    else:
        print("   ❌ Generated: MISSING")
    
    # Check if reference exists
    for ref in refs:
        ref_path = os.path.join(REFERENCES_DIR, ref)
        if os.path.exists(ref_path):
            print(f"   ✅ Reference: {ref}")
        else:
            print(f"   ❌ Reference: {ref} MISSING")

print("\n" + "=" * 60)
print("COMPARISON COMPLETE")
print("=" * 60)
print("""
To manually compare:
1. Open ComfyUI: https://mu1aafo3zo4da1-8188.proxy.runpod.net
2. Use IP-Adapter with reference images for exact matches
3. For each character, load their reference image first

Next steps:
- Review generated images
- Identify mismatches
- Regenerate with IP-Adapter for consistency
""")

# Save comparison list
with open("/mnt/c/Users/fbmor/broken-spire-comparison/validation_report.txt", "w") as f:
    f.write("BROKEN SPIRE - CHARACTER CONSISTENCY REPORT\n")
    f.write("=" * 50 + "\n\n")
    for frame, refs in COMPARISONS.items():
        f.write(f"{frame}\n")
        f.write(f"  References: {', '.join(refs)}\n")
        gen_path = os.path.join(GENERATED_DIR, f"{frame}.png")
        f.write(f"  Generated: {'✅ EXISTS' if os.path.exists(gen_path) else '❌ MISSING'}\n\n")

print("Report saved to: /mnt/c/Users/fbmor/broken-spire-comparison/validation_report.txt")