import requests
import json
import time

BASE_URL = "http://localhost:5000/api/agent/chat-dev"

def test_query(query, description):
    print(f"\n{'='*60}")
    print(f"TEST: {description}")
    print(f"QUERY: '{query}'")
    print(f"{'='*60}")
    
    payload = {
        "user_query": query,
    }
    
    try:
        start_time = time.time()
        response = requests.post(BASE_URL, json=payload, timeout=60)
        elapsed = time.time() - start_time
        
        if response.status_code == 200:
            data = response.json()
            print(f"✅ Success ({elapsed:.2f}s)")
            print(f"🤖 Response: {data.get('ai_response')}")
            p_ids = data.get('product_ids', [])
            print(f"📦 Product IDs ({len(p_ids)}): {', '.join(p_ids[:5])}{'...' if len(p_ids)>5 else ''}")
            
            # Check diagnostics if available (some versions return it)
            if "diagnostics" in data:
                for d in data["diagnostics"]:
                    print(f"   - {d.get('label')}: {d.get('elapsed_ms')}ms")
        else:
            print(f"❌ Error {response.status_code}: {response.text}")
            
    except Exception as e:
        print(f"⚠️ Connection failed: {e}")

if __name__ == "__main__":
    print("🚀 Starting Canifa AI Stylist API Verification...")
    
    # 1. Test Product Search (SQLite Mock Logic)
    test_query("Tìm áo phông cho bé trai", "Product Search (SQLite Mock)")
    
    # 2. Test Promotion (StarRocks Production)
    test_query("Hôm nay có khuyến mãi gì hot không?", "Promotion Search (StarRocks)")
    
    # 3. Test Knowledge (StarRocks Production)
    test_query("Chính sách đổi trả của Canifa thế nào?", "Knowledge Search (StarRocks)")
    
    # 4. Test Store (StarRocks Production)
    test_query("Tìm cửa hàng ở Cầu Giấy", "Store Search (StarRocks)")
    
    print("\n✅ Verification sequence completed.")
