#!/usr/bin/env python3
"""
Test brand extraction in actual ProductsMasterService
"""
import sys
import asyncio
sys.path.append('backend')

from app.core.database import get_db
from app.services.products_master_service import ProductsMasterService

async def test_service_brand_extraction():
    """Test brand extraction in real service environment"""
    print("Testing brand extraction in ProductsMasterService...")
    
    try:
        # Create service instance
        service = ProductsMasterService()
        
        # Get database session and initialize extractors
        async for db in get_db():
            await service.initialize_extractors(db)
            
            # Test cases that should work with our fixes
            test_cases = [
                "Tods女款卡其色麻花拖鞋 XXW70K0GU70MIDM033美国代购8.15 LA",
                "SportMax女款黑色羊皮皮衣GEL 003 代购8.18",
                "小王国 ST JOHN 粉色羊毛+羊绒 大衣 K60GN32 8.17LA",
                "小王国Stuart Weitzman女款黑色牛皮+羊毛短靴CHRLI CZY SJ787 LA",
                "小王国S MaxMara女款深灰色羊毛双排扣短大衣 CAPE 002代购8.18"
            ]
            
            expected_results = [
                "Tods",
                "SportMax", 
                "ST JOHN",
                "Stuart Weitzman",
                "S MaxMara"
            ]
            
            print("\nTesting brand extraction in service:")
            print("=" * 60)
            
            passed = 0
            for i, (test_name, expected) in enumerate(zip(test_cases, expected_results), 1):
                if service.brand_extractor:
                    result = service.brand_extractor.extract_brand(test_name)
                    print(f"\nTest {i}:")
                    print(f"Input: {test_name}")
                    print(f"Expected: {expected}")
                    print(f"Got: {result}")
                    
                    if result == expected:
                        print("✅ PASS")
                        passed += 1
                    else:
                        print("❌ FAIL")
                else:
                    print(f"Test {i}: ❌ BrandExtractor not initialized!")
            
            print(f"\n{'='*60}")
            print(f"Service test results: {passed}/{len(test_cases)} passed")
            
            # Also check if the service has the correct brand_aliases
            if hasattr(service, '_brand_aliases_cache'):
                print(f"\nBrand aliases loaded: {len(service._brand_aliases_cache)} entries")
            
            break
            
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    asyncio.run(test_service_brand_extraction())