#!/usr/bin/env python3
"""
Check current data status in the system
"""
import sys
import asyncio
sys.path.append('backend')

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

async def check_data_status():
    """Check the current data status in the system"""
    print("Checking current data status...")
    
    try:
        # Get database session
        async for db in get_db():
            # Check raw orders count
            raw_count = await db.scalar(select(func.count(RawOrder.id)))
            print(f"Raw orders count: {raw_count}")
            
            # Check products master count
            products_count = await db.scalar(select(func.count(ProductMaster.id)))
            print(f"Products master count: {products_count}")
            
            # Check if there are any sample raw orders
            if raw_count > 0:
                sample_raw = await db.execute(select(RawOrder).limit(3))
                raw_orders = sample_raw.scalars().all()
                print(f"\nSample raw orders:")
                for i, order in enumerate(raw_orders, 1):
                    print(f"{i}. ID: {order.id}")
                    print(f"   Product Name: {getattr(order, '线上宝贝名称', 'N/A')}")
                    print(f"   Merchant Code: {getattr(order, '线上商家编码', 'N/A')}")
                    print("")
            
            # Check if there are any sample products
            if products_count > 0:
                sample_products = await db.execute(select(ProductMaster).limit(3))
                products = sample_products.scalars().all()
                print(f"\nSample products:")
                for i, product in enumerate(products, 1):
                    print(f"{i}. ID: {product.id}")
                    print(f"   Product Name: {product.product_name}")
                    print(f"   Product Code: {product.product_code}")
                    print(f"   Brand: {product.brand}")
                    print("")
            
            break
            
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()

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