#!/usr/bin/env python3
"""
KPI Validation Layer
Overrides GPT scores when KPIs prove them impossible
Prevents glaring mistakes like scoring 5/5 for Guest when there's no building
"""

def validate_criteria_scores(criteria_scores, kpis):
    """
    Validate and override GPT scores based on hard KPI facts

    Args:
        criteria_scores: dict with keys like 'market_garden', 'guest_accommodation', etc.
        kpis: dict with 'land_size_m2', 'building_size_m2', 'bedrooms', 'bathrooms'

    Returns:
        dict: validated criteria scores with overrides applied
    """
    validated = criteria_scores.copy()

    # Extract KPIs (handle None/null values)
    # IMPORTANT: None means "unknown" not "zero" - don't override based on missing data
    land_size = kpis.get('land_size_m2')
    building_size = kpis.get('building_size_m2')
    bedrooms = kpis.get('bedrooms')
    bathrooms = kpis.get('bathrooms')

    # Track what we override for logging
    overrides = []

    # Rule 1: EXPLICIT ZERO building (not None) → Guest=1, Rental=1, Workshop=1
    # Only override if we have explicit data proving there's no building
    if building_size is not None and building_size == 0 and bedrooms is not None and bedrooms == 0:
        if validated.get('guest_accommodation', 0) > 1:
            overrides.append(f"Guest {validated.get('guest_accommodation')}→1 (confirmed no building)")
            validated['guest_accommodation'] = 1

        if validated.get('rental_units', 0) > 1:
            overrides.append(f"Rental {validated.get('rental_units')}→1 (confirmed no building)")
            validated['rental_units'] = 1

        if validated.get('workshop', 0) > 1:
            overrides.append(f"Workshop {validated.get('workshop')}→1 (confirmed no building)")
            validated['workshop'] = 1

    # Rule 2: Very small building → Workshop ≤ 2
    elif building_size is not None and building_size > 0 and building_size < 30:
        if validated.get('workshop', 0) > 2:
            overrides.append(f"Workshop {validated.get('workshop')}→2 (building too small: {building_size}m²)")
            validated['workshop'] = 2

    # Rule 3: EXPLICIT ZERO bedrooms (not None) → Guest=1, Rental=1
    if bedrooms is not None and bedrooms == 0:
        if validated.get('guest_accommodation', 0) > 1:
            overrides.append(f"Guest {validated.get('guest_accommodation')}→1 (confirmed no bedrooms)")
            validated['guest_accommodation'] = 1

        if validated.get('rental_units', 0) > 1:
            overrides.append(f"Rental {validated.get('rental_units')}→1 (confirmed no bedrooms)")
            validated['rental_units'] = 1

    # Rule 4: 1 bedroom only → Guest ≤ 2, Rental=1
    elif bedrooms is not None and bedrooms == 1:
        if validated.get('guest_accommodation', 0) > 2:
            overrides.append(f"Guest {validated.get('guest_accommodation')}→2 (only 1 bedroom)")
            validated['guest_accommodation'] = 2

        if validated.get('rental_units', 0) > 1:
            overrides.append(f"Rental {validated.get('rental_units')}→1 (only 1 bedroom, not enough for multiple units)")
            validated['rental_units'] = 1

    # Rule 5: 2 bedrooms → Rental ≤ 2 (not enough for profitable multi-unit)
    elif bedrooms is not None and bedrooms == 2:
        if validated.get('rental_units', 0) > 2:
            overrides.append(f"Rental {validated.get('rental_units')}→2 (only 2 bedrooms)")
            validated['rental_units'] = 2

    # Rule 6: Small land → Market Garden ≤ 2
    if land_size is not None and land_size > 0 and land_size < 1500:
        if validated.get('market_garden', 0) > 2:
            overrides.append(f"Market Garden {validated.get('market_garden')}→2 (land too small: {land_size}m²)")
            validated['market_garden'] = 2

    # Rule 7: Very small land → Market Garden=1
    if land_size is not None and land_size > 0 and land_size < 500:
        if validated.get('market_garden', 0) > 1:
            overrides.append(f"Market Garden {validated.get('market_garden')}→1 (land too small: {land_size}m²)")
            validated['market_garden'] = 1

    # Rule 8: No land info but description suggests "only land" → Building-dependent ≤ 2
    # This would require checking the summary text - we'll add this later

    return validated, overrides


def validate_property_scores(property_data):
    """
    Validate all scores for a single property

    Args:
        property_data: dict with 'criteria', 'land_size_m2', 'building_size_m2', 'bedrooms', 'bathrooms'

    Returns:
        dict: property_data with validated criteria scores
    """
    kpis = {
        'land_size_m2': property_data.get('land_size_m2'),
        'building_size_m2': property_data.get('building_size_m2'),
        'bedrooms': property_data.get('bedrooms'),
        'bathrooms': property_data.get('bathrooms')
    }

    original_criteria = property_data.get('criteria', {})
    validated_criteria, overrides = validate_criteria_scores(original_criteria, kpis)

    # Update property data
    property_data['criteria'] = validated_criteria

    # Store overrides for transparency
    if overrides:
        property_data['validation_overrides'] = overrides

    return property_data


def validate_all_properties(enriched_data):
    """
    Validate scores for all properties in enriched_data.json

    Args:
        enriched_data: list of property dicts

    Returns:
        list: validated properties with overrides applied
    """
    total_overrides = 0

    for prop in enriched_data:
        original_criteria = prop.get('criteria', {}).copy()
        validated_prop = validate_property_scores(prop)

        if 'validation_overrides' in validated_prop:
            total_overrides += len(validated_prop['validation_overrides'])

    print(f"✅ Validated {len(enriched_data)} properties")
    print(f"🔧 Applied {total_overrides} score overrides based on KPIs")

    return enriched_data


if __name__ == "__main__":
    import json
    import sys

    # Load enriched data
    try:
        with open('enriched_data.json', 'r', encoding='utf-8') as f:
            data = json.load(f)
    except FileNotFoundError:
        print("❌ enriched_data.json not found")
        sys.exit(1)

    print(f"📊 Validating {len(data)} properties...")
    print("=" * 60)

    # Validate all properties
    validated_data = validate_all_properties(data)

    # Show examples of overrides
    print("\n📋 Example overrides:")
    override_count = 0
    for prop in validated_data:
        if 'validation_overrides' in prop:
            print(f"\n{prop['url']}")
            print(f"  Price: €{prop.get('price', 0):,.0f}")
            print(f"  KPIs: {prop.get('bedrooms', 0)} bed, {prop.get('building_size_m2', 0)} m² building, {prop.get('land_size_m2', 0)} m² land")
            for override in prop['validation_overrides']:
                print(f"  ⚠️  {override}")
            override_count += 1
            if override_count >= 5:  # Show first 5 examples
                break

    if override_count == 0:
        print("  (No overrides needed - all scores align with KPIs!)")

    # Save validated data
    with open('enriched_data.json', 'w', encoding='utf-8') as f:
        json.dump(validated_data, f, ensure_ascii=False, indent=2)

    print("\n✅ Validation complete - enriched_data.json updated")
