#!/usr/bin/env python3
"""
Debug S MaxMara specifically
"""
import sys
sys.path.append('backend')

from app.utils.text_parser import BrandExtractor

def debug_s_maxmara():
    """Debug S MaxMara extraction"""
    extractor = BrandExtractor()
    
    test_name = "小王国S MaxMara女款深灰色羊毛双排扣短大衣 CAPE 002代购8.18"
    
    print(f"Input: {test_name}")
    print("")
    
    # Show preprocessing
    preprocessed_name = test_name
    brand_replacements = {
        'S MaxMara': 'S MaxMara',
    }
    
    for pattern, replacement in brand_replacements.items():
        if pattern in preprocessed_name:
            preprocessed_name = preprocessed_name.replace(pattern, replacement)
            print(f"Replacement applied: {pattern} -> {replacement}")
    
    preprocessed_name = preprocessed_name.replace('&', ' & ')
    print(f"After preprocessing: {preprocessed_name}")
    
    # Show word splitting
    words = preprocessed_name.strip().split()
    print(f"Words: {words}")
    print("")
    
    # Check ignore keywords
    ignore_keywords = {'小王国', '现货', 'GN现货', '国现', '代购', '美国代购', '女款', '男款', '中性款', 'the', 'cube', 'studio', '主线'}
    
    for i, word in enumerate(words):
        clean_word = word.strip('，。！？；：""''()（）[]【】{}〈〉《》')
        print(f"Word {i}: '{word}' -> cleaned: '{clean_word}'")
        
        if clean_word in ignore_keywords:
            print(f"  → Ignored (in ignore_keywords)")
            continue
            
        # Check if word would be processed
        if len(clean_word) > 1 and not clean_word.isdigit():
            print(f"  → Valid candidate")
            
            # Check combinations
            if i + 1 < len(words):
                next_word = words[i + 1].strip('，。！？；：""''()（）[]【】{}〈〉《》')
                if (len(next_word) > 1 and not next_word.isdigit() and 
                    next_word not in ignore_keywords):
                    combined = f"{clean_word} {next_word}"
                    print(f"    Combined with next: '{combined}'")
                    
                    # Check known brands
                    known_multi_brands = [
                        'ST JOHN', 'Alexander Wang', 'Alexander McQueen', 
                        'Stuart Weitzman', 'Rag & Bone', 'MaxMara Studio',
                        'Roger Vivier', 'Jil Sander', 'Ralph Lauren',
                        'S MaxMara'
                    ]
                    
                    for known_brand in known_multi_brands:
                        if (known_brand.lower() == combined.lower() or
                            combined.lower().startswith(known_brand.lower())):
                            print(f"      → Matches known brand: {known_brand}")
                            break
            break
        else:
            print(f"  → Too short or is digit")
    
    # Final result
    result = extractor.extract_brand(test_name)
    print(f"\nFinal result: {result}")

if __name__ == "__main__":
    debug_s_maxmara()