import os
import re
import sys

# Ensure UTF-8 output on Windows
sys.stdout.reconfigure(encoding='utf-8')

def test_m1_screenplay():
    base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    tap01_path = os.path.join(base_dir, 'FilmMaker', 'TAP_01_XUAN_SAC_THE_NGUYEN_VA_GIONG_BAO_DOAN_TRUONG.md')
    idx_path = os.path.join(base_dir, 'FilmMaker', 'INDEX_VA_DANH_MUC_CANH_QUAY.md')
    sb_path = os.path.join(base_dir, '03_Storyboards', 'Ep01_Gia_Bien_Storyboard.md')
    epguide_path = os.path.join(base_dir, '01_Scripts_And_Episodes', 'EPISODE_GUIDE_30MIN.md')

    with open(tap01_path, 'r', encoding='utf-8') as f:
        tap01 = f.read()
    with open(idx_path, 'r', encoding='utf-8') as f:
        idx = f.read()
    with open(sb_path, 'r', encoding='utf-8') as f:
        sb = f.read()
    with open(epguide_path, 'r', encoding='utf-8') as f:
        epguide = f.read()

    # 1. Total shots in TAP_01
    shots = re.findall(r'Shot \d+: .*(ep01_scene\d+_shot\d+)', tap01)
    assert len(shots) == 188, f'Expected 188 shots, got {len(shots)}'
    assert len(set(shots)) == 188, f'Expected 188 unique shots, got {len(set(shots))}'

    # 2. Shot count by scene
    expected_scene_counts = {
        1: 21, 2: 15, 3: 15, 4: 12,
        5: 27, 6: 14, 7: 8, 8: 8, 9: 6,
        10: 14, 11: 10, 12: 8, 13: 12, 14: 12, 15: 6
    }
    for sc_num, expected_cnt in expected_scene_counts.items():
        sc_shots = [s for s in shots if f'ep01_scene{sc_num:02d}_' in s]
        assert len(sc_shots) == expected_cnt, f'Scene {sc_num} expected {expected_cnt}, got {len(sc_shots)}'

    # 3. Acts breakdown
    act1 = sum(expected_scene_counts[i] for i in range(1, 5))
    act2 = sum(expected_scene_counts[i] for i in range(5, 10))
    act3 = sum(expected_scene_counts[i] for i in range(10, 16))
    assert act1 == 63, f'Act 1: {act1}'
    assert act2 == 63, f'Act 2: {act2}'
    assert act3 == 62, f'Act 3: {act3}'

    # 4. Đạm Tiên sequence (Scene 05)
    assert 'CẢNH 05-A' in tap01 and 'tiểu khê' in tap01.lower()
    assert 'CẢNH 05-B' in tap01 and 'sè sè nấm đất' in tap01.lower()
    assert 'CẢNH 05-C' in tap01 and 'đau đớn thay phận đàn bà' in tap01.lower()
    assert 'dấu hài' in tap01.lower() or 'dấu giày' in tap01.lower()

    # 5. Lip-Sync Guard in Scene 08 Shot 08
    assert '[KHÓA KHẨU HÌNH BẮT BUỘC]: Đôi môi nhân vật trong khung hình khép chặt tự nhiên khi có tiếng gọi từ xa' in tap01

    # 6. Male crying purge
    assert 'Kim Trọng' in tap01 and 'cơ hàm bạnh chặt' in tap01
    assert 'nghẹn ngào rơi nước mắt' not in tap01
    assert 'tiếng Vương Ông kêu nghẹn ngào' not in sb
    assert 'Tiếng khóc than xé lòng của gia đình Vương viên ngoại' not in epguide
    assert 'tiếng thét nghẹn ngào của Vương Ông' not in tap01
    assert 'Gào lên tuyệt vọng' not in tap01

    # 7. INDEX check
    assert '15 Phân cảnh | 188 Shots | Thời lượng: 29 phút 40 giây' in idx
    assert '56 Phân cảnh điện ảnh lớn' in idx

    print('PASS: test_m1_screenplay completed successfully (188 shots, 15 scenes, 3 acts, 0 male crying, lip-sync guard verified).')

if __name__ == '__main__':
    test_m1_screenplay()
