# tests/test_refined_icon_system.py

import pandas as pd
import sys
sys.path.append('..')  # Adjust as needed

from fmatch.engine import _get_individual_column_stats
from gui import FuzzyMatcherApp  # Your GUI class

def test_stats_gathering():
    """Test that stats function returns pure data"""
    test_data = {
        'customer_id': ['C001', 'C002', 'C003', 'C004'],
        'email': ['john@example.com', 'jane@test.com', None, 'bob@gmail.com'],
        'company': ['Acme Corp', 'Acme Corp', 'Tech Inc', 'Tech Inc'],
        'sparse_col': [None, None, None, 'value'],
    }
    
    df = pd.DataFrame(test_data)
    
    print("Testing pure statistics gathering:\n")
    
    for col in df.columns:
        stats = _get_individual_column_stats(df[col])
        
        print(f"\nColumn: {col}")
        print(f"  Fill rate: {stats['fill_rate']:.2%}")
        print(f"  Distinct ratio: {stats['distinct_ratio']:.2%}")
        print(f"  Is primary ID: {stats['is_primary_id']}")
        print(f"  ID score: {stats['id_score']}")
        print(f"  Inferred type: {stats['inferred_type']}")
        print(f"  Has email pattern: {stats['has_email_pattern']}")
        
        # Verify stats are pure data (no UI decisions)
        assert 'icons' not in stats
        assert 'tooltips' not in stats
        assert isinstance(stats['fill_rate'], float)

def test_gui_hints():
    """Test that GUI makes correct icon decisions"""
    app = FuzzyMatcherApp()  # Create GUI instance
    
    test_cases = [
        # (stats_dict, expected_icons)
        ({
            'is_primary_id': True,
            'id_confidence': 0.95,
            'fill_rate': 1.0,
            'distinct_ratio': 1.0,
        }, ['🔑']),
        
        ({
            'inferred_type': 'email',
            'has_email_pattern': True,
            'fill_rate': 0.9,
            'distinct_ratio': 0.8,
        }, ['📧']),
        
        ({
            'fill_rate': 0.2,
            'distinct_ratio': 0.1,
            'has_data': True,
        }, ['⚠️']),
        
        ({
            'fill_rate': 0.85,
            'distinct_ratio': 0.3,
            'entropy': 3.5,
            'inferred_type': 'unknown',
        }, ['🧲']),
    ]
    
    print("\nTesting GUI hint generation:\n")
    
    for stats, expected in test_cases:
        hints = app._get_column_ui_hints(stats)
        print(f"Stats: {stats}")
        print(f"  Icons: {hints['icons']}")
        print(f"  Expected: {expected}")
        print(f"  Tooltips: {hints['tooltips']}")
        
        # Verify expected icon is present
        for icon in expected:
            assert icon in hints['icons'], f"Expected {icon} not found"
        print("  ✓ Passed\n")

if __name__ == '__main__':
    test_stats_gathering()
    test_gui_hints()
    print("\nAll tests passed! ✅")