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("🚀 Testing Canifa AI Stylist Tags Logic...")
    
    # Test tags map
    test_query("Tìm quần âu nam mặc đi làm công sở", "Test Tag: Đi làm công sở")
    test_query("áo thun mặc ở nhà cho nữ", "Test Tag: Ở nhà / mặc ngủ")
    
    print("\n✅ Verification sequence completed.")
