#!/usr/bin/env python3
"""
Check the exact product mentioned by user
"""
import sys
import asyncio
sys.path.append('backend')

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

async def check_exact_product():
    """Check the exact problematic product"""
    print("检查具体的问题产品...")
    
    try:
        async for db in get_db():
            # Search for the exact product
            exact_name = "Tods女款牛仔拼色镂空锁扣装饰踩鞋XXW00G0GW40R43ZZEU代购8.15LA"
            
            # Try different search methods
            print(f"\n搜索产品名称: {exact_name}")
            print("=" * 80)
            
            # Method 1: Exact match
            exact_query = select(ProductMaster).where(
                ProductMaster.线上宝贝名称 == exact_name
            )
            
            exact_result = await db.execute(exact_query)
            exact_products = exact_result.scalars().all()
            
            if exact_products:
                print(f"\n✅ 精确匹配找到 {len(exact_products)} 个产品:")
                for product in exact_products:
                    print(f"\n产品ID: {product.id}")
                    print(f"产品名称: {product.线上宝贝名称}")
                    print(f"品牌: '{product.品牌}'")
                    print(f"品牌长度: {len(product.品牌)} 字符")
                    print(f"货号: '{product.货号}'")
                    print(f"创建时间: {product.created_at}")
                    print(f"更新时间: {product.updated_at}")
            else:
                print("❌ 精确匹配未找到产品")
            
            # Method 2: Partial match
            partial_query = select(ProductMaster).where(
                ProductMaster.线上宝贝名称.like('%牛仔拼色镂空锁扣装饰踩鞋XXW00G0GW40R43ZZEU%')
            )
            
            partial_result = await db.execute(partial_query)
            partial_products = partial_result.scalars().all()
            
            if partial_products:
                print(f"\n部分匹配找到 {len(partial_products)} 个产品:")
                for i, product in enumerate(partial_products, 1):
                    print(f"\n{i}. 产品ID: {product.id}")
                    print(f"   产品名称: {product.线上宝贝名称}")
                    print(f"   品牌: '{product.品牌}'")
                    print(f"   品牌长度: {len(product.品牌)} 字符")
                    print(f"   货号: '{product.货号}'")
            
            # Method 3: Search by product code
            code_query = select(ProductMaster).where(
                ProductMaster.货号 == 'XXW00G0GW40R43ZZEU'
            )
            
            code_result = await db.execute(code_query)
            code_products = code_result.scalars().all()
            
            if code_products:
                print(f"\n通过货号找到 {len(code_products)} 个产品:")
                for i, product in enumerate(code_products, 1):
                    print(f"\n{i}. 产品ID: {product.id}")
                    print(f"   产品名称: {product.线上宝贝名称}")
                    print(f"   品牌: '{product.品牌}'")
                    print(f"   品牌长度: {len(product.品牌)} 字符")
                    print(f"   货号: '{product.货号}'")
            
            # Check for any product with brand containing the full description
            bad_brand = "Tods牛仔拼色镂空锁扣装饰踩鞋XXW00G0GW40R43ZZEU8.15LA"
            bad_brand_query = select(ProductMaster).where(
                ProductMaster.品牌 == bad_brand
            )
            
            bad_result = await db.execute(bad_brand_query)
            bad_products = bad_result.scalars().all()
            
            if bad_products:
                print(f"\n❌ 发现有问题的品牌名 '{bad_brand}':")
                for product in bad_products:
                    print(f"\n产品ID: {product.id}")
                    print(f"产品名称: {product.线上宝贝名称}")
                    print(f"品牌: '{product.品牌}'")
                    print(f"货号: '{product.货号}'")
            else:
                print(f"\n✅ 没有产品使用问题品牌名 '{bad_brand}'")
            
            # Search for products with overly long Tods brand names
            long_tods_query = select(ProductMaster).where(
                ProductMaster.品牌.like('Tods%'),
                ProductMaster.品牌 != 'Tods'
            )
            
            long_tods_result = await db.execute(long_tods_query)
            long_tods_products = long_tods_result.scalars().all()
            
            if long_tods_products:
                print(f"\n发现 {len(long_tods_products)} 个Tods品牌名异常的产品:")
                for i, product in enumerate(long_tods_products, 1):
                    print(f"\n{i}. 产品ID: {product.id}")
                    print(f"   产品名称: {product.线上宝贝名称[:60]}...")
                    print(f"   异常品牌: '{product.品牌}'")
                    print(f"   品牌长度: {len(product.品牌)} 字符")
                    print(f"   货号: '{product.货号}'")
            
            break
            
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()

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