#!/usr/bin/env python
"""
Test script to verify the batch product orders API is returning the correct sales attributes
"""
import asyncio
import sys
import json
from pathlib import Path

# Add the app directory to Python path
sys.path.append(str(Path(__file__).parent / "app"))

from app.core.database import get_db, init_db
from app.services.procurement_list_service_v2 import ProcurementListServiceV2

async def test_batch_api():
    """Test the batch product orders API"""
    
    # Initialize database
    await init_db()
    
    # Create service
    service = ProcurementListServiceV2()
    
    # Get database session
    async for db in get_db():
        try:
            # Test with Anine Bing product that user is seeing
            product_names = ["Anine Bing 女款Karter Jogger米灰色小logo 卫裤A-03-10598-GRY1"]
            procurement_method = "NY"
            
            print(f"测试批量API - 商品: {product_names}")
            print(f"采购方式: {procurement_method}")
            print("-" * 50)
            
            result = await service.get_batch_product_orders(
                db=db,
                product_names=product_names,
                procurement_method=procurement_method
            )
            
            print("API返回结果:")
            print(f"总商品数: {result.get('total_products', 0)}")
            print(f"总SKU数: {result.get('total_skus', 0)}")
            print()
            
            if result.get('products'):
                for product in result['products']:
                    print(f"商品名称: {product['product_name']}")
                    print(f"SKU数量: {len(product.get('orders', []))}")
                    
                    for order in product.get('orders', [])[:3]:  # 只显示前3个
                        print(f"  - 尺寸: {order.get('size', 'N/A')}")
                        print(f"    销售属性: {order.get('sales_attr', 'N/A')}")
                        print(f"    待采购数量: {order.get('pending_procurement_qty', 0)}")
                        print()
            else:
                print("❌ 没有返回商品数据")
            
        except Exception as e:
            print(f"❌ 错误: {e}")
            import traceback
            traceback.print_exc()
        
        finally:
            await db.close()
            break

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