# test_integration_simple.py - Updated version
import pandas as pd
import logging

# Correct import path - engine is in core subdirectory
from core.engine import preprocess, MatchConfig

# Enable logging
logging.basicConfig(level=logging.INFO)

# Test data
df = pd.DataFrame({
    'company_name': ['Acme Inc', 'TechCorp', 'DataCo'],
    'email': ['john@gmail.com', 'info@techcorp.com', 'contact@yahoo.com'],
    'domain': ['gmail.com', 'techcorp.com', 'yahoo.com']
})

print("=" * 60)
print("Testing Preprocessing System")
print("=" * 60)

# Test 1: Old system (feature flag OFF)
print("\n1. Testing OLD system (feature flag OFF)...")
config_old = MatchConfig(
    job_id="test_old",
    src_path="dummy",
    ref_path="dummy",
    use_new_preprocessing=False  # Old system
)

result_old = preprocess(
    df=df.copy(),
    block_key_length=3,
    ruleset_name="default",
    gui_checkbox_rules=[{"action": "lower", "columns": "__ALL_STRINGS__"}],
    strip_b2c_domains=True,
    config=config_old
)

print("\nOld system result:")
print(result_old)
print(f"Columns: {list(result_old.columns)}")

# Test 2: New system (feature flag ON)
print("\n" + "=" * 60)
print("\n2. Testing NEW system (feature flag ON)...")
config_new = MatchConfig(
    job_id="test_new",
    src_path="dummy",
    ref_path="dummy",
    use_new_preprocessing=True  # New system!
)

try:
    result_new = preprocess(
        df=df.copy(),
        block_key_length=3,
        ruleset_name="default",
        gui_checkbox_rules=[{"action": "lower", "columns": "__ALL_STRINGS__"}],
        strip_b2c_domains=True,
        config=config_new
    )
    print("\nNew system result:")
    print(result_new)
    print(f"Columns: {list(result_new.columns)}")
    
    # Compare results
    print("\n" + "=" * 60)
    print("COMPARISON:")
    
    # Check if both systems produce similar results
    if result_old.shape == result_new.shape:
        print("✓ Same shape")
    else:
        print(f"✗ Different shapes: old={result_old.shape}, new={result_new.shape}")
    
    # Check columns
    old_cols = set(result_old.columns)
    new_cols = set(result_new.columns)
    if old_cols == new_cols:
        print("✓ Same columns")
    else:
        print(f"✗ Different columns:")
        print(f"  Only in old: {old_cols - new_cols}")
        print(f"  Only in new: {new_cols - old_cols}")
    
    # Check B2C filtering
    if 'domain' in result_old.columns and 'domain' in result_new.columns:
        print(f"\nB2C filtering comparison:")
        print(f"Old: {result_old['domain'].tolist()}")
        print(f"New: {result_new['domain'].tolist()}")
    
    print("\n✅ SUCCESS: New preprocessing system is working!")
    
except Exception as e:
    print(f"\n❌ ERROR with new system: {e}")
    import traceback
    traceback.print_exc()

print("\n" + "=" * 60)