"""
اختبار الـ API مباشرة عبر HTTP
"""
import requests
import json

BASE_URL = "http://localhost:8000/api/v1"

# تسجيل الدخول
print("1. Logging in...")
login_response = requests.post(
    f"{BASE_URL}/auth/login",
    json={"email": "ithelpdesk@ic.gov.sa", "password": "Dt@1234567"}
)
print(f"Login status: {login_response.status_code}")

if login_response.status_code == 200:
    token = login_response.json().get("access_token")
    print(f"Got token: {token[:30]}...")
    
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }
    
    # اختبار SMTP
    print("\n2. Testing SMTP endpoint...")
    smtp_response = requests.post(
        f"{BASE_URL}/settings/smtp/test",
        headers=headers,
        json={
            "recipient_email": "ashraffarid@gmail.com",
            "smtp_host": "mail.ai4ksa.com",
            "smtp_port": 465,
            "smtp_username": "ithelpdesk@ai4ksa.com",
            "smtp_password": "ithelpdesk@123",
            "smtp_use_ssl": True,
            "smtp_use_tls": False
        }
    )
    print(f"SMTP test status: {smtp_response.status_code}")
    print(f"SMTP test response: {smtp_response.text}")
    
    # اختبار التنبيه التجريبي
    print("\n3. Testing Alert endpoint...")
    alert_response = requests.post(
        f"{BASE_URL}/settings/smtp/test-alert",
        headers=headers,
        json={
            "recipient_email": "ashraffarid@gmail.com",
            "alert_type": "threshold",
            "device_name": "Room3",
            "reading_type": "temperature",
            "current_value": 17,
            "threshold_value": 10,
            "condition": "above",
            "severity": "warning"
        }
    )
    print(f"Alert test status: {alert_response.status_code}")
    print(f"Alert test response: {alert_response.text}")
else:
    print(f"Login failed: {login_response.text}")
    print("\nPlease update the email/password in this script!")
