import asyncio
import io
import sys

import httpx

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

API_URL = "http://localhost:5000/api/agent/chat-lead-flow"

async def main():
    payload = {"user_query": "tìm sịp nam 7 màu"}
    headers = {"X-Device-ID": "test_device_sip"}
    
    try:
        async with httpx.AsyncClient(timeout=60.0) as client:
            response = await client.post(API_URL, json=payload, headers=headers)
            data = response.json()
            
            print(f"\n🤖 Bot: {data.get('ai_response', '')}")
            products = data.get("products", [])
            
            if products:
                print(f"\n📦 Products returned ({len(products)}):")
                for p in products[:5]:
                    print(f"   - [{p.get('sku', '?')}] {p.get('name', '?')} | {p.get('product_line', '?')}")
            else:
                print("\n📦 No products returned.")
                
    except Exception as e:
        print(f"Error: {e}")

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