#!/usr/bin/env python3
"""
Test the fix for '微瑕！' prefix in brand names
"""
import sys
sys.path.append('backend')

from app.utils.text_parser import BrandExtractor

def test_weixia_fix():
    """Test the fix for quality descriptors in brand names"""
    extractor = BrandExtractor()
    
    test_cases = [
        {
            "name": "微瑕！小王国 Tods 蓝色字母刺绣棒球帽 X9MF3499050WFTU808  LA",
            "expected": "Tods",
            "description": "微瑕！ prefix should be excluded"
        },
        {
            "name": "轻微瑕疵 MaxMara 红色羊毛大衣 ABC123 代购8.18",
            "expected": "MaxMara", 
            "description": "轻微瑕疵 prefix should be excluded"
        },
        {
            "name": "二手 SportMax 黑色皮衣 DEF456 代购MC",
            "expected": "SportMax",
            "description": "二手 prefix should be excluded"
        },
        {
            "name": "九成新 Burberry 经典风衣 GHI789 代购LA",
            "expected": "Burberry",
            "description": "九成新 prefix should be excluded"
        },
        {
            "name": "瑕疵品 小王国 ST JOHN 粉色套装 JKL012 代购MC",
            "expected": "ST JOHN",
            "description": "瑕疵品 prefix should be excluded"
        }
    ]
    
    print("Testing Quality Descriptor Fix")
    print("=" * 60)
    
    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
        candidates = extractor._extract_brand_candidates(case["name"])
        print(f"Candidates: {candidates}")
        
        if result == expected:
            print("✅ PASS")
            passed += 1
        else:
            print("❌ FAIL")
    
    print(f"\n{'='*60}")
    print(f"Results: {passed}/{total} tests passed ({passed/total*100:.1f}%)")
    
    return passed == total

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