#!/usr/bin/env python3
"""
Test script to regenerate products with new smart selection logic
"""
import sys
import os
import asyncio
sys.path.append('backend')

from app.core.database import get_db, init_db
from app.services.products_master_service import ProductsMasterService

async def test_regenerate():
    """Test regenerating products with new logic"""
    print("Starting product regeneration with smart selection logic...")
    
    try:
        # Initialize database
        await init_db()
        
        # Create service instance
        service = ProductsMasterService()
        
        # Get database session
        async for db in get_db():
            # Regenerate products
            result = await service.generate_products_master(db, force_rebuild=True)
            
            print(f"\n处理结果:")
            print(f"返回数据结构: {result.keys()}")
            for key, value in result.items():
                print(f"{key}: {value}")
            
            # Get some sample products to verify the new logic
            from sqlalchemy import select, text
            from app.models.products_master import ProductMaster
            
            sample_query = select(ProductMaster).limit(10)
            sample_result = await db.execute(sample_query)
            products = sample_result.scalars().all()
            
            print(f"\n前10个产品示例:")
            for i, product in enumerate(products, 1):
                print(f"{i}. 产品名称: {product.product_name}")
                print(f"   货号: {product.product_code or '无'}")
                print(f"   品牌: {product.brand or '无'}")
                print("")
            
            break
            
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()

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