"""اختبار API إرسال الإيميل"""
import requests

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:
    print(f"Login failed: {login_response.text}")
    exit(1)

token = login_response.json().get("access_token")
print(f"Token: {token[:50]}...")

headers = {"Authorization": f"Bearer {token}"}

# اختبار SMTP Test
print("\n2. Testing SMTP Test 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_from_email": "ithelpdesk@ai4ksa.com",
        "smtp_from_name": "SmartLife",
        "smtp_use_tls": False,
        "smtp_use_ssl": True
    }
)
print(f"SMTP Test status: {smtp_response.status_code}")
print(f"SMTP Test response: {smtp_response.text}")

# اختبار Test Alert
print("\n3. Testing Test 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": "مستشعر درجة الحرارة - اختبار",
        "reading_type": "temperature",
        "current_value": 35.5,
        "threshold_value": 30,
        "condition": "above",
        "severity": "high"
    }
)
print(f"Test Alert status: {alert_response.status_code}")
print(f"Test Alert response: {alert_response.text}")
