#!/usr/bin/env python3
"""
Test script to demonstrate the difference between old and new confidence calculations
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.core.config import DEFAULT_FACE_TOLERANCE, USE_CALIBRATED_CONFIDENCE, CONFIDENCE_CALIBRATION_METHOD
from src.core.face_processing import FaceProcessor
from src.core.database import DatabaseManager

def test_confidence_calibration():
    """Test and compare old vs new confidence calculations"""
    
    print("🔍 Confidence Calibration Test")
    print("=" * 50)
    
    # Initialize face processor (we don't need database for this test)
    db_manager = DatabaseManager(":memory:")  # In-memory database for testing
    face_processor = FaceProcessor(db_manager, verbose=1)
    
    # Test different distance values
    test_distances = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.2, 1.5, 2.0]
    tolerance = DEFAULT_FACE_TOLERANCE
    
    print(f"Tolerance threshold: {tolerance}")
    print(f"Calibration enabled: {USE_CALIBRATED_CONFIDENCE}")
    print(f"Calibration method: {CONFIDENCE_CALIBRATION_METHOD}")
    print()
    
    print("Distance | Old Linear | New Calibrated | Difference | Description")
    print("-" * 70)
    
    for distance in test_distances:
        # Old linear calculation
        old_confidence = (1 - distance) * 100
        
        # New calibrated calculation
        new_confidence, description = face_processor._get_calibrated_confidence(distance, tolerance)
        
        difference = new_confidence - old_confidence
        
        print(f"{distance:8.1f} | {old_confidence:10.1f}% | {new_confidence:13.1f}% | {difference:+9.1f}% | {description}")
    
    print()
    print("📊 Key Differences:")
    print("- Old method: Simple linear transformation (1 - distance) * 100")
    print("- New method: Empirical calibration based on DeepFace ArcFace characteristics")
    print("- New method provides more realistic match probabilities")
    print()
    
    # Test different calibration methods
    print("🔧 Testing Different Calibration Methods:")
    print("-" * 50)
    
    # Temporarily change calibration method to test different approaches
    original_method = CONFIDENCE_CALIBRATION_METHOD
    
    test_distance = 0.4  # Example distance
    print(f"Distance: {test_distance}, Tolerance: {tolerance}")
    print()
    
    methods = ["linear", "sigmoid", "empirical"]
    for method in methods:
        # Update the global config (this is just for testing)
        import src.core.config
        src.core.config.CONFIDENCE_CALIBRATION_METHOD = method
        
        confidence, desc = face_processor._get_calibrated_confidence(test_distance, tolerance)
        print(f"{method:10}: {confidence:6.1f}% - {desc}")
    
    # Restore original method
    src.core.config.CONFIDENCE_CALIBRATION_METHOD = original_method

if __name__ == "__main__":
    test_confidence_calibration()



