#!/usr/bin/env python3
"""
Check S MaxMara brand recognition issues
"""
import sys
import asyncio
sys.path.append('backend')

from sqlalchemy import select, func, text
from app.core.database import get_db
from app.models.products_master import ProductMaster

async def check_s_maxmara_issues():
    """Check S MaxMara brand recognition issues"""
    print("Checking S MaxMara brand recognition...")
    
    try:
        async for db in get_db():
            # Look for S MaxMara products
            s_maxmara_query = select(ProductMaster).where(
                ProductMaster.线上宝贝名称.like('%S MaxMara%')
            ).limit(15)
            
            s_maxmara_result = await db.execute(s_maxmara_query)
            s_maxmara_products = s_maxmara_result.scalars().all()
            
            if s_maxmara_products:
                print(f"Found {len(s_maxmara_products)} S MaxMara products:")
                for i, product in enumerate(s_maxmara_products, 1):
                    print(f"{i}. Product: {product.线上宝贝名称}")
                    print(f"   Current Brand: '{product.品牌}'")
                    print(f"   Product Code: '{product.货号 or 'None'}'")
                    print("")
            
            # Also check for potential misidentified MaxMara products
            maxmara_long_query = select(ProductMaster).where(
                ProductMaster.品牌.like('%MaxMara%') &
                (func.length(ProductMaster.品牌) > 20)
            ).limit(10)
            
            maxmara_long_result = await db.execute(maxmara_long_query)
            long_brands = maxmara_long_result.scalars().all()
            
            if long_brands:
                print(f"\nFound {len(long_brands)} MaxMara products with long brand names:")
                for i, product in enumerate(long_brands, 1):
                    print(f"{i}. Product: {product.线上宝贝名称[:80]}...")
                    print(f"   Brand: '{product.品牌}'")
                    print("")
            
            break
            
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()

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