import os
from pathlib import Path
from PIL import Image

PROJECT_ROOT = Path(".").resolve()
VIDEOS_DIR = PROJECT_ROOT / "04_Assets" / "videos"
KEYFRAMES_DIR = PROJECT_ROOT / "04_Assets" / "keyframes"
ARCHIVE_DIR = PROJECT_ROOT / "04_Assets" / "archive" / "ep01_legacy_v1"
CHARACTERS_DIR = PROJECT_ROOT / "04_Assets" / "characters"

print("--- 1. KEYFRAMES CHECK ---")
all_kf = os.listdir(KEYFRAMES_DIR)
ep01_kf_dirs = [d for d in all_kf if (KEYFRAMES_DIR / d).is_dir() and d.lower().startswith("ep01")]
ep01_kf_files = [f for f in all_kf if (KEYFRAMES_DIR / f).is_file() and "ep01" in f.lower()]
non_ep01_kf_dirs = [d for d in all_kf if (KEYFRAMES_DIR / d).is_dir() and not d.lower().startswith("ep01")]
non_ep01_kf_files = [f for f in all_kf if (KEYFRAMES_DIR / f).is_file() and "ep01" not in f.lower()]
print(f"ep01_scene10_shot14 exists: {(KEYFRAMES_DIR / 'ep01_scene10_shot14').exists()}")
print(f"Total ep01 kf dirs: {len(ep01_kf_dirs)} -> {ep01_kf_dirs}")
print(f"Total ep01 kf files: {len(ep01_kf_files)} -> {ep01_kf_files}")
print(f"Non-ep01 kf dirs count: {len(non_ep01_kf_dirs)}")
print(f"Non-ep01 kf files count: {len(non_ep01_kf_files)}")

print("\n--- 2. VIDEOS CHECK ---")
all_videos = os.listdir(VIDEOS_DIR)
ep01_videos = [v for v in all_videos if "ep01" in v.lower()]
non_ep01_videos = [v for v in all_videos if "ep01" not in v.lower()]
print(f"Total ep01 videos in 04_Assets/videos: {len(ep01_videos)} -> {ep01_videos}")
print(f"Total non-ep01 videos count: {len(non_ep01_videos)}")

print("\n--- 3. ARCHIVE CHECK ---")
archived_files = os.listdir(ARCHIVE_DIR)
archived_mp4 = [f for f in archived_files if f.lower().endswith(".mp4")]
total_archive_size = sum((ARCHIVE_DIR / f).stat().st_size for f in archived_mp4)
print(f"Total archived mp4 count: {len(archived_mp4)}")
print(f"Total archived mp4 size: {total_archive_size} bytes ({total_archive_size / (1024**3):.3f} GB)")

# Check ftyp headers
invalid_ftyp = []
for f in archived_mp4:
    with open(ARCHIVE_DIR / f, "rb") as fp:
        hdr = fp.read(8)
        if len(hdr) < 8 or hdr[4:8] != b"ftyp":
            invalid_ftyp.append(f)
print(f"Invalid ftyp count: {len(invalid_ftyp)}")

readme = ARCHIVE_DIR / "README.md"
print(f"README.md exists: {readme.exists()}")
if readme.exists():
    print(f"README.md size: {readme.stat().st_size} bytes")

print("\n--- 4. CHARACTERS CHECK ---")
char_count = sum(len(files) for _, _, files in os.walk(CHARACTERS_DIR))
print(f"Total character files: {char_count}")

CORE_EP01_CHARACTERS = [
    ("01_Main_Protagonists/thuy_kieu_maiden_16yo_720p.png", (1280, 720)),
    ("01_Main_Protagonists/thuy_kieu_maiden_16yo_master.png", (1376, 768)),
    ("01_Main_Protagonists/thuy_van_maiden_16yo_720p.png", (1280, 720)),
    ("01_Main_Protagonists/thuy_van_maiden_16yo_master.png", (1376, 768)),
    ("01_Main_Protagonists/kim_trong_18yo_720p.png", (1280, 720)),
    ("01_Main_Protagonists/kim_trong_18yo_master.png", (1376, 768)),
    ("02_Vuong_Family_And_Fate/vuong_quan_16yo_720p.png", (1280, 720)),
    ("02_Vuong_Family_And_Fate/vuong_quan_16yo_master.png", (1376, 768)),
    ("02_Vuong_Family_And_Fate/vuong_ong_55yo_720p.png", (1280, 720)),
    ("02_Vuong_Family_And_Fate/vuong_ong_55yo_master.png", (1376, 768)),
    ("02_Vuong_Family_And_Fate/vuong_ba_50yo_720p.png", (1280, 720)),
    ("02_Vuong_Family_And_Fate/vuong_ba_50yo_master.png", (1376, 768)),
    ("02_Vuong_Family_And_Fate/dam_tien_720p.png", (1280, 720)),
    ("02_Vuong_Family_And_Fate/dam_tien_master.png", (1376, 768)),
]
bad_chars = []
for rel, dims in CORE_EP01_CHARACTERS:
    p = CHARACTERS_DIR / rel
    if not p.exists():
        bad_chars.append((rel, "missing"))
    else:
        with Image.open(p) as img:
            if img.size != dims or img.format != "PNG":
                bad_chars.append((rel, f"bad dims/format: {img.size}, {img.format}"))
print(f"Bad core portraits: {bad_chars}")
