import sys
import os
import pandas as pd
import json

# Add src to path so we can import fmatch
sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'src'))

# Import your engine
try:
    from fmatch.core.engine import *  # Import everything from engine
    print("✅ Successfully imported fmatch.core.engine")
except ImportError as e:
    print(f"❌ Error importing engine: {e}")
    sys.exit(1)

class TestFMatchAccuracy:
    """Test fmatch with Apollo test data"""
    
    def __init__(self):
        # Load config
        with open('config/test_data_paths.json', 'r') as f:
            self.config = json.load(f)
    
    def test_matching(self, dataset_size='small'):
        """Test matching on a dataset"""
        print(f"\n{'='*60}")
        print(f"TESTING FMATCH - {dataset_size.upper()} DATASET")
        print(f"{'='*60}")
        
        # Load test data
        dataset = self.config['test_datasets'][dataset_size]
        crm_df = pd.read_csv(dataset['crm_data'])
        foreign_df = pd.read_csv(dataset['foreign_data'])
        
        print("\nLoaded data:")
        print(f"  CRM records: {len(crm_df):,}")
        print(f"  Foreign records: {len(foreign_df):,}")
        
        # Prepare data for your engine
        # Assuming your engine expects certain column names
        # Adjust these based on what your engine expects
        
        # Rename columns to match what your engine expects
        crm_df_renamed = crm_df.rename(columns={
            'Account Name': 'company_name',
            'Domain (website)': 'domain'
        })
        
        foreign_df_renamed = foreign_df.rename(columns={
            'Company / Account': 'company_name',
            'Domain': 'domain'
        })
        
        # TODO: Call your actual matching function
        # This will depend on how your engine.py is structured
        # Common patterns:
        
        # Option 1: If you have a match function
        # start_time = time.time()
        # matches = match(crm_df_renamed, foreign_df_renamed)
        # elapsed = time.time() - start_time
        
        # Option 2: If you have a class
        # matcher = FuzzyMatcher()  # or whatever your class is called
        # start_time = time.time()
        # matches = matcher.match(crm_df_renamed, foreign_df_renamed)
        # elapsed = time.time() - start_time
        
        # For now, let's see what's available in your engine
        print("\nAvailable in engine module:")
        engine_contents = [item for item in dir() if not item.startswith('_')]
        for item in engine_contents[:20]:  # Show first 20 items
            print(f"  - {item}")
        
        # Baseline test - exact domain matching
        print("\n=== BASELINE: Exact Domain Matching ===")
        crm_domains = set(crm_df[crm_df['Domain (website)'].notna()]['Domain (website)'].str.lower())
        foreign_domains = set(foreign_df[foreign_df['Domain'].notna()]['Domain'].str.lower())
        
        exact_matches = crm_domains & foreign_domains
        print(f"Exact domain matches: {len(exact_matches):,}")
        print("Expected match rate: ~40% (built into test data)")
        print(f"Actual match rate: {len(exact_matches)/len(foreign_domains)*100:.1f}%")
        
        return len(exact_matches)

def main():
    """Run all tests"""
    tester = TestFMatchAccuracy()
    
    # Test on different dataset sizes
    for size in ['small', 'medium', 'large']:
        matches = tester.test_matching(size)
        print(f"\nResults for {size}: {matches:,} exact domain matches")

if __name__ == "__main__":
    main()
