import os
import csv

def verify_preprocessing(datapath='./data/'):
    print("Scanning CSV splits to find all expected videos...")
    
    domains = ['human', 'animal', 'cartoon']
    splits = ['train', 'test']
    tensor_dir = os.path.join(datapath, "processed_tensors")
    
    # We use a list instead of a set now because every combined name is unique!
    expected_files = []
    
    # 1. Read every CSV and reconstruct the exact filename saved by the preprocessor
    for dom in domains:
        for split in splits:
            csv_path = os.path.join(datapath, f"HAC_Splits/HAC_{split}_only_{dom}.csv")
            
            if not os.path.exists(csv_path):
                print(f"⚠️ Warning: Could not find split file: {csv_path}")
                continue
                
            with open(csv_path, 'r') as f:
                reader = csv.reader(f)
                for row in reader:
                    video_id = row[0]
                    # This matches the new 'domain_videoid.npz' naming format
                    expected_filename = f"{dom}_{video_id}.npz"
                    expected_files.append(expected_filename)
                    
    total_expected = len(expected_files)
    print(f"Found {total_expected} total video entries across all CSV splits.")
    print(f"Checking {tensor_dir} for matching .npz files...\n")
    
    if not os.path.exists(tensor_dir):
        print(f"❌ ERROR: The directory '{tensor_dir}' does not exist yet. Run your preprocessing script first!")
        return
        
    # 2. Check the hard drive for the corresponding prefixed .npz files
    missing_files = []
    for filename in expected_files:
        npz_path = os.path.join(tensor_dir, filename)
        if not os.path.exists(npz_path):
            missing_files.append(filename)
            
    # 3. Report the final findings
    if len(missing_files) == 0:
        print("✅ SUCCESS: Every single video was perfectly processed and saved with its domain prefix!")
        print(f"Total .npz files verified on disk: {total_expected} / {total_expected}")
    else:
        print(f"❌ WARNING: {len(missing_files)} out of {total_expected} videos are missing!")
        print("-" * 60)
        print("First 15 missing files (Run preprocess_offline.py again to generate them):")
        for f in missing_files[:15]:
            print(f" -> {f}")
        print("-" * 60)

if __name__ == '__main__':
    verify_preprocessing(datapath='./data/')