#!/usr/bin/env python3
"""Test script for denes-router-classifier package."""

import time
import sys
from pathlib import Path

# Add src to path for local testing (before installation)
sys.path.insert(0, str(Path(__file__).parent / "src"))

from denes_router_classifier import classify_domain, classify_batch, __version__

print(f"🧪 Testing denes-router-classifier v{__version__}\n")

# Test cases from different domains
test_cases = [
    ("Busca el clima en Asunción", "Web_Search"),
    ("No tengo internet", "TI"),
    ("¿Cuál es la capital de Francia?", "Generic"),
    ("Mi computadora no enciende", "TI"),
    ("Encuentra información sobre Python", "Web_Search"),
    ("Explica qué es machine learning", "Generic"),
]

print("=" * 70)
print("TEST 1: Single Classification")
print("=" * 70)

total_time = 0
for text, expected in test_cases:
    start = time.perf_counter()
    result = classify_domain(text, threshold=0.7)
    latency = (time.perf_counter() - start) * 1000
    total_time += latency

    status = "✅" if result.primary == expected else "❌"
    print(f"{status} '{text}'")
    print(f"   → {result.primary} (confidence: {result.confidence:.4f})")
    print(f"   → Latency: {latency:.2f}ms")
    print()

avg_latency = total_time / len(test_cases)
print(f"📊 Average latency: {avg_latency:.2f}ms")
print(f"{'✅ PASS' if avg_latency < 100 else '⚠️ SLOW'}: Target < 100ms\n")

print("=" * 70)
print("TEST 2: Multi-label Classification")
print("=" * 70)

multi_label_text = "Busca ayuda para mi PC"
result = classify_domain(multi_label_text, threshold=0.5, multi_label=True)

print(f"Text: '{multi_label_text}'")
print(f"Primary: {result.primary} (confidence: {result.confidence:.4f})")
if result.all_predictions:
    print("All predictions:")
    for pred in result.all_predictions:
        print(f"  - {pred.domain}: {pred.confidence:.4f}")
print()

print("=" * 70)
print("TEST 3: Batch Classification")
print("=" * 70)

batch_texts = [text for text, _ in test_cases]
start = time.perf_counter()
results = classify_batch(batch_texts, threshold=0.7)
batch_time = (time.perf_counter() - start) * 1000

print(f"Batch size: {len(batch_texts)}")
print(f"Total time: {batch_time:.2f}ms")
print(f"Per-item: {batch_time / len(batch_texts):.2f}ms")
print()

for text, result, (_, expected) in zip(batch_texts, results, test_cases):
    status = "✅" if result.primary == expected else "❌"
    print(f"{status} {result.primary} (expected: {expected})")

print()
print("=" * 70)
print("TEST 4: Low Confidence (Generic fallback)")
print("=" * 70)

low_conf_text = "Hmm..."
result = classify_domain(low_conf_text, threshold=0.7)

print(f"Text: '{low_conf_text}'")
print(f"Primary: {result.primary} (confidence: {result.confidence:.4f})")
print(f"Includes Generic: {result.includes_generic}")
print(f"{'✅ PASS' if result.includes_generic else '❌ FAIL'}: Should include Generic\n")

print("=" * 70)
print("✅ ALL TESTS COMPLETED")
print("=" * 70)
