#!/usr/bin/env python3
"""
Test the fixed brand extraction logic
"""
import sys
sys.path.append('backend')

from app.utils.text_parser import BrandExtractor

def test_brand_extraction():
    """Test brand extraction with problematic cases"""
    extractor = BrandExtractor()
    
    test_cases = [
        {
            "name": "小王国 ST JOHN 粉色羊毛+羊绒 大衣 K60GN32 8.17LA",
            "expected": "ST JOHN",
            "description": "Should extract ST JOHN as combined brand"
        },
        {
            "name": "小王国Stuart Weitzman女款黑色牛皮+羊毛短靴CHRLI CZY SJ787 LA",
            "expected": "Stuart Weitzman", 
            "description": "Should extract Stuart Weitzman as combined brand"
        },
        {
            "name": "小王国 Rag & Bone绿色短款圆领短袖T恤MOSS MAI BOXY TEE8.13",
            "expected": "Rag & Bone",
            "description": "Should extract Rag & Bone as combined brand"
        },
        {
            "name": "小王国MaxMara Studio 纯羊毛格纹系带大衣ALCADE 010代购8.18",
            "expected": "MaxMara Studio",
            "description": "Should extract MaxMara Studio correctly"
        },
        {
            "name": "小王国MaxMara the cube短款双排扣风衣DTRENCH 013代购8.18",
            "expected": "MaxMara",
            "description": "Should extract MaxMara correctly"
        },
        {
            "name": "小王国MaxMara主线红色龙年限定双排扣大衣ADDURRE 011代购8.18",
            "expected": "MaxMara",
            "description": "Should extract MaxMara and not the long description"
        },
        {
            "name": "Tods 黑色Mini做旧麻花扣马鞍包 XBWAOYJ0100RORB999 美国代购MC",
            "expected": "Tods",
            "description": "Should extract Tods correctly"
        },
        {
            "name": "小王国Roger Vivier女款黄色方扣中跟鞋RVW00 黄色 37.5",
            "expected": "Roger Vivier",
            "description": "Should extract Roger Vivier as combined brand"
        }
    ]
    
    print("Testing Fixed Brand Extraction Logic")
    print("=" * 50)
    
    passed = 0
    total = len(test_cases)
    
    for i, case in enumerate(test_cases, 1):
        result = extractor.extract_brand(case["name"])
        expected = case["expected"]
        
        print(f"\nTest {i}: {case['description']}")
        print(f"Input: {case['name']}")
        print(f"Expected: {expected}")
        print(f"Got: {result}")
        
        # Debug: show candidates and preprocessing
        candidates = extractor._extract_brand_candidates(case["name"])
        print(f"Candidates: {candidates}")
        
        # Show preprocessing result for debugging
        preprocessed_name = case["name"]
        brand_replacements = {
            'MaxMara the cube': 'MaxMara',
            'MaxMara主线': 'MaxMara',
            'Rag&Bone': 'Rag & Bone',
        }
        for pattern, replacement in brand_replacements.items():
            if pattern in preprocessed_name:
                preprocessed_name = preprocessed_name.replace(pattern, replacement)
        preprocessed_name = preprocessed_name.replace('&', ' & ')
        print(f"Preprocessed: {preprocessed_name}")
        
        if result == expected:
            print("✅ PASS")
            passed += 1
        else:
            print("❌ FAIL")
    
    print(f"\n{'='*50}")
    print(f"Results: {passed}/{total} tests passed ({passed/total*100:.1f}%)")
    
    return passed == total

if __name__ == "__main__":
    success = test_brand_extraction()
    sys.exit(0 if success else 1)