#!/usr/bin/env python3
"""
Test to ensure validation fix persists
Verifies that missing KPI data doesn't force all scores to 1
"""
import pandas as pd
import sys

def test_score_diversity():
    """Test that scores show diversity, not all 1.0"""
    print("🧪 Testing Score Diversity...")
    
    df = pd.read_csv('analysis_output.csv')
    
    # Calculate averages
    guest_avg = df['guest_accommodation'].mean()
    workshop_avg = df['workshop'].mean()
    rental_avg = df['rental_units'].mean()
    
    print(f"   Guest avg: {guest_avg:.2f}")
    print(f"   Workshop avg: {workshop_avg:.2f}")
    print(f"   Rental avg: {rental_avg:.2f}")
    
    # Check if averages are NOT all 1.0 (which would indicate broken validation)
    if guest_avg <= 1.1:
        print("   ❌ FAIL: Guest scores stuck at ~1.0 (validation broken!)")
        return False
    
    if workshop_avg <= 1.1:
        print("   ❌ FAIL: Workshop scores stuck at ~1.0 (validation broken!)")
        return False
    
    if rental_avg <= 1.1:
        print("   ❌ FAIL: Rental scores stuck at ~1.0 (validation broken!)")
        return False
    
    # Check for reasonable diversity (should have range > 1)
    guest_range = df['guest_accommodation'].max() - df['guest_accommodation'].min()
    workshop_range = df['workshop'].max() - df['workshop'].min()
    rental_range = df['rental_units'].max() - df['rental_units'].min()
    
    if guest_range < 2:
        print(f"   ❌ FAIL: Guest scores have no diversity (range: {guest_range})")
        return False
    
    if workshop_range < 2:
        print(f"   ❌ FAIL: Workshop scores have no diversity (range: {workshop_range})")
        return False
    
    if rental_range < 2:
        print(f"   ❌ FAIL: Rental scores have no diversity (range: {rental_range})")
        return False
    
    print("   ✅ PASS: Scores show proper diversity")
    return True

def test_no_excessive_overrides():
    """Test that validation doesn't override most properties"""
    print("\n🧪 Testing Validation Overrides...")
    
    # Read last parse_criteria run output
    # For now, just check the CSV doesn't have suspiciously uniform scores
    df = pd.read_csv('analysis_output.csv')
    
    # Count properties with identical Guest/Workshop/Rental = 1
    all_ones = df[
        (df['guest_accommodation'] == 1.0) & 
        (df['workshop'] == 1.0) & 
        (df['rental_units'] == 1.0)
    ]
    
    percentage_ones = (len(all_ones) / len(df)) * 100
    
    print(f"   Properties with all scores = 1: {len(all_ones)}/{len(df)} ({percentage_ones:.1f}%)")
    
    if percentage_ones > 50:
        print("   ❌ FAIL: Over 50% of properties have uniform scores = 1")
        print("   This indicates validation is forcing scores down inappropriately")
        return False
    
    print("   ✅ PASS: Validation not over-correcting")
    return True

def test_scores_match_gpt_text():
    """Test that stored scores match what's in GPT analysis text"""
    print("\n🧪 Testing Score Extraction Accuracy...")
    
    df = pd.read_csv('analysis_output.csv')
    
    # Get a sample property with GPT analysis
    sample = df[df['GPT Analyse'].notna()].iloc[0]
    gpt_text = str(sample['GPT Analyse'])
    
    # Check if the text contains scoring patterns
    if 'Gastenverblijf:' in gpt_text or 'gastenverblijf:' in gpt_text.lower():
        stored_guest = sample['guest_accommodation']
        
        # If GPT text mentions guest > 1, stored score shouldn't be 1
        if any(f": {i}" in gpt_text for i in [2, 3, 4, 5]) and stored_guest == 1.0:
            print(f"   ❌ FAIL: GPT text shows Guest > 1, but stored = {stored_guest}")
            return False
    
    print("   ✅ PASS: Scores appear to match GPT analysis")
    return True

if __name__ == '__main__':
    print("=" * 60)
    print("🔍 VALIDATION FIX REGRESSION TEST")
    print("=" * 60)
    
    all_pass = True
    
    try:
        all_pass &= test_score_diversity()
        all_pass &= test_no_excessive_overrides()
        all_pass &= test_scores_match_gpt_text()
    except Exception as e:
        print(f"\n❌ ERROR: Test failed with exception: {e}")
        all_pass = False
    
    print("\n" + "=" * 60)
    if all_pass:
        print("✅ ALL TESTS PASSED - Validation fix is working correctly")
        print("=" * 60)
        sys.exit(0)
    else:
        print("❌ TESTS FAILED - Validation may have regressed")
        print("=" * 60)
        print("\n💡 To fix: Check validate_scores.py for None handling")
        print("   Ensure it uses 'is not None' checks before comparing to 0")
        sys.exit(1)
