"""Quick test: analyze 20 samples to verify everything works before full batch."""
import sys
import os
import time
import warnings

warnings.filterwarnings("ignore")

PROJECT = r"C:\Users\Administrator\Documents\fl_control"
os.chdir(PROJECT)
sys.path.insert(0, PROJECT)

from src.analyzer import collect_audio_files, batch_analyze

lib1 = os.path.join(PROJECT, "libreria", "reggaeton")
lib2 = os.path.join(PROJECT, "librerias", "reggaeton")

files = collect_audio_files(lib1, lib2)
print(f"Total files: {len(files)}")

# Take first 20
test_files = files[:20]
print(f"Testing with {len(test_files)} files...\n")

start = time.time()
results = batch_analyze(test_files, workers=8)
elapsed = time.time() - start

valid = [r for r in results if "error" not in r]
errors = [r for r in results if "error" in r]

print(f"\nDone in {elapsed:.1f}s ({elapsed/len(test_files):.2f}s/file)")
print(f"Valid: {len(valid)} | Errors: {len(errors)}")

for r in valid:
    role = r["role"]
    char = r["character"]
    key = r["musical"]["key"]
    bpm = r["perceptual"]["tempo"]
    new = r["new_name"]
    orig = os.path.basename(r["original_path"])
    print(f"  {orig:45s} -> {role:10s} {char:12s} {key:5s} {bpm:5.0f}bpm -> {new}")

if errors:
    print(f"\nErrors:")
    for e in errors:
        print(f"  {e}")

print(f"\n{'OK - Ready for full batch!' if len(valid) > 15 else 'Too many errors!'}")
