#!/usr/bin/env python3
"""
Test the specific Tods brand extraction issue
"""
import sys
import asyncio
sys.path.append('backend')

from app.utils.text_parser import BrandExtractor
from app.core.database import get_db
from app.services.products_master_service import ProductsMasterService
from sqlalchemy import select, func, text
from app.models.products_master import ProductMaster

async def test_specific_tods_issue():
    """Test the specific problematic Tods product"""
    print("Testing specific Tods brand extraction issue...")
    
    # The specific problematic case
    problem_case = "Tods女款牛仔拼色镂空锁扣装饰踩鞋XXW00G0GW40R43ZZEU代购8.15LA"
    expected_brand = "Tods"
    
    # Test with standalone extractor
    print("\n=== Testing with standalone BrandExtractor ===")
    extractor = BrandExtractor()
    
    result = extractor.extract_brand(problem_case)
    print(f"Input: {problem_case}")
    print(f"Expected: {expected_brand}")
    print(f"Got: {result}")
    print(f"Result: {'✅ PASS' if result == expected_brand else '❌ FAIL'}")
    
    # Debug the extraction process
    print("\n=== Debug Information ===")
    candidates = extractor._extract_brand_candidates(problem_case)
    print(f"Brand candidates: {candidates}")
    
    # Show preprocessing steps
    import re
    preprocessed = problem_case
    print(f"Original: {preprocessed}")
    
    # Show Chinese-English separation
    preprocessed = re.sub(r'([A-Za-z])([\u4e00-\u9fff])', r'\1 \2', preprocessed)
    preprocessed = re.sub(r'([\u4e00-\u9fff])([A-Za-z])', r'\1 \2', preprocessed)
    print(f"After separation: {preprocessed}")
    
    # Test with service
    print("\n=== Testing with ProductsMasterService ===")
    try:
        service = ProductsMasterService()
        async for db in get_db():
            await service.initialize_extractors(db)
            
            if service.brand_extractor:
                service_result = service.brand_extractor.extract_brand(problem_case)
                print(f"Service result: {service_result}")
                print(f"Service result: {'✅ PASS' if service_result == expected_brand else '❌ FAIL'}")
            else:
                print("❌ BrandExtractor not initialized in service")
            
            break
            
    except Exception as e:
        print(f"Service test error: {e}")
        import traceback
        traceback.print_exc()
    
    # Check database for this specific product
    print("\n=== Checking Database ===")
    try:
        async for db in get_db():
            # Look for products with similar names
            query = select(ProductMaster).where(
                ProductMaster.线上宝贝名称.like('%Tods%牛仔拼色%')
            )
            
            result = await db.execute(query)
            products = result.scalars().all()
            
            print(f"Found {len(products)} matching products in database:")
            for i, product in enumerate(products, 1):
                print(f"\n{i}. 产品名称: {product.线上宝贝名称}")
                print(f"   品牌: '{product.品牌}'")
                print(f"   货号: '{product.货号}'")
                print(f"   品牌长度: {len(product.品牌)} 字符")
                
                # Check if this is the problematic one
                if product.品牌 != "Tods" and "Tods" in product.品牌:
                    print(f"   ❌ 这是问题产品! 品牌应该是 'Tods' 但得到了 '{product.品牌}'")
            
            break
            
    except Exception as e:
        print(f"Database check error: {e}")
        import traceback
        traceback.print_exc()
    
    # Test similar cases
    print("\n=== Testing Similar Cases ===")
    similar_cases = [
        "Tods女款卡其色麻花拖鞋 XXW70K0GU70MIDM033美国代购8.15 LA",
        "Tods男款黑色皮鞋ABC123代购8.16MC",
        "Tods女装牛仔拼色锁扣装饰鞋DEF456代购8.17LA"
    ]
    
    for case in similar_cases:
        result = extractor.extract_brand(case)
        print(f"Input: {case}")
        print(f"Brand: {result}")
        print(f"Status: {'✅' if result == 'Tods' else '❌'}")
        print()

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