"""
TEST LIMIT_INFO IN RESPONSE - Quick Test
Kiểm tra xem response có trả về limit_info hay không
"""

import json
from datetime import datetime

import requests

API_URL = "http://localhost:5000/api/agent/chat-dev"
DEVICE_ID = f"test_device_{datetime.now().strftime('%H%M%S')}"

print("=" * 60)
print("🧪 TEST: limit_info in response")
print("=" * 60)
print(f"📱 Device ID: {DEVICE_ID}")
print(f"🌐 API URL: {API_URL}")
print()

headers = {
    "Authorization": DEVICE_ID,  # Guest - device_id as auth token
    "Content-Type": "application/json",
}

payload = {"user_query": "Xin chào", "images": []}

print("📤 Sending request...")
print(f"   Headers: {json.dumps(headers, indent=2)}")
print(f"   Payload: {json.dumps(payload, indent=2)}")
print()

try:
    response = requests.post(API_URL, json=payload, headers=headers, timeout=30)

    print(f"📥 Response Status: {response.status_code}")
    print()

    if response.status_code == 200:
        data = response.json()

        print("✅ Response JSON:")
        print(json.dumps(data, indent=2, ensure_ascii=False))
        print()

        # Check limit_info
        print("🔍 Checking limit_info...")
        if "limit_info" in data:
            limit_info = data["limit_info"]
            print("✅ limit_info EXISTS!")
            print(f"   📊 Limit: {limit_info.get('limit')}")
            print(f"   📈 Used: {limit_info.get('used')}")
            print(f"   📉 Remaining: {limit_info.get('remaining')}")

            # Validate fields
            if all(k in limit_info for k in ["limit", "used", "remaining"]):
                print()
                print("✅✅✅ TEST PASSED! All fields present.")
            else:
                print()
                print("⚠️ Missing fields in limit_info!")
        else:
            print("❌ limit_info MISSING in response!")
            print()
            print("Available keys:", list(data.keys()))

    else:
        print("❌ Error Response:")
        print(response.text)

except requests.exceptions.Timeout:
    print("⏰ Request timeout after 30s!")
except requests.exceptions.ConnectionError:
    print("❌ Connection failed! Is server running on port 5000?")
except Exception as e:
    print(f"❌ Error: {e}")
    import traceback

    traceback.print_exc()

print()
print("=" * 60)
print("Test completed!")
print("=" * 60)
