"""
Test Recall Flow via API.
"""
import asyncio
import io
import sys

import httpx

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

CONVERSATION = [
    "tìm áo ngọ nghiêng",
    "có quần nào phối với áo đó không",
    "cho cái áo người lớn cơ mà",
    "mình thích cái polo, giá bao nhiêu vậy",
    "còn size nào cho mình cao 1m70 65kg",
]

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

async def main():
    device_id = "test_device_recall_api"
    headers = {"X-Device-ID": device_id}
    
    async with httpx.AsyncClient(timeout=60.0) as client:
        for i, query in enumerate(CONVERSATION):
            print(f"\n{'='*60}")
            print(f"👤 Turn {i+1}: {query}")
            print(f"{'='*60}")
            
            payload = {"user_query": query}
            
            try:
                response = await client.post(API_URL, json=payload, headers=headers)
                data = response.json()
                
                print(f"\n🤖 Bot: {data.get('ai_response', '')[:400]}")
                
                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('sku_code', '?'))}] {p.get('name', '?')}")
                else:
                    print("\n📦 No products returned.")
                    
                insight = data.get("user_insight")
                if insight:
                    print(f"\n💾 LATEST_PRODUCT_INTEREST: {insight.get('LATEST_PRODUCT_INTEREST', '')[:200]}")
            except Exception as e:
                print(f"Error calling API: {e}")

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