#!/usr/bin/env python3
"""
Force regenerate products with complete cleanup
"""
import sys
import asyncio
sys.path.append('backend')

from app.core.database import get_db, init_db
from app.services.products_master_service import ProductsMasterService
from sqlalchemy import select, func, text
from app.models.products_master import ProductMaster

async def force_regenerate_products():
    """Force regenerate all products with cleanup"""
    print("Force regenerating products with complete cleanup...")
    
    try:
        # Initialize database
        await init_db()
        
        # Create service instance
        service = ProductsMasterService()
        
        # Get database session
        async for db in get_db():
            # Check current product count
            current_count = await db.scalar(select(func.count(ProductMaster.id)))
            print(f"Current products in database: {current_count}")
            
            # Force rebuild with complete cleanup
            print("\nStarting forced rebuild...")
            result = await service.generate_products_master(db, force_rebuild=True)
            
            print(f"\n强制重新生成结果:")
            for key, value in result.items():
                print(f"{key}: {value}")
            
            # Check new count
            new_count = await db.scalar(select(func.count(ProductMaster.id)))
            print(f"\nNew products count: {new_count}")
            
            # Sample some products to verify brand extraction
            print("\n验证品牌提取结果:")
            
            # Check specific problem brands
            test_brands = ['Tods', 'SportMax', 'ST JOHN', 'Stuart Weitzman', 'MaxMara']
            
            for brand in test_brands:
                count_query = select(func.count(ProductMaster.id)).where(
                    ProductMaster.品牌 == brand
                )
                count = await db.scalar(count_query)
                print(f"{brand}: {count} products")
                
                if count > 0:
                    # Show a sample
                    sample_query = select(ProductMaster).where(
                        ProductMaster.品牌 == brand
                    ).limit(1)
                    sample = await db.execute(sample_query)
                    product = sample.scalar_one_or_none()
                    if product:
                        print(f"  Sample: {product.线上宝贝名称[:50]}...")
            
            # Check for problematic long brand names
            print(f"\n检查问题品牌名:")
            long_brand_query = select(ProductMaster).where(
                func.length(ProductMaster.品牌) > 20
            ).limit(3)
            
            long_brands = await db.execute(long_brand_query)
            problem_products = long_brands.scalars().all()
            
            if problem_products:
                print("发现问题品牌:")
                for product in problem_products:
                    print(f"  品牌: '{product.品牌}' (长度: {len(product.品牌)})")
                    print(f"  产品: {product.线上宝贝名称[:50]}...")
                    print()
            else:
                print("未发现明显的问题品牌名！")
            
            break
            
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()

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