"""
اختبار إرسال إيميل باستخدام SendGrid API
"""
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Email, To, Content, HtmlContent

# Load API key from env file
from dotenv import load_dotenv
load_dotenv('sendgrid.env')

SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY', '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')

def send_test_email():
    print("=" * 50)
    print("Testing SendGrid Email")
    print("=" * 50)
    
    # يجب استخدام إيميل مُحقق في SendGrid
    from_email = "ithelpdesk@ai4ksa.com"  # أو أي إيميل مُحقق
    to_email = "ashraffarid@gmail.com"
    
    print(f"From: {from_email}")
    print(f"To: {to_email}")
    print()
    
    message = Mail(
        from_email=from_email,
        to_emails=to_email,
        subject='SmartLife - Test Email via SendGrid',
        html_content='''
        <html>
        <body dir="rtl" style="font-family: Arial, sans-serif;">
            <h2>🎉 تم بنجاح!</h2>
            <p>هذا بريد إلكتروني تجريبي عبر SendGrid API.</p>
            <p>إعدادات البريد الإلكتروني تعمل بشكل صحيح.</p>
            <hr>
            <p style="color: #666; font-size: 12px;">SmartLife Monitoring System</p>
        </body>
        </html>
        '''
    )
    
    try:
        sg = SendGridAPIClient(SENDGRID_API_KEY)
        response = sg.send(message)
        print(f"Status Code: {response.status_code}")
        print(f"Response Body: {response.body}")
        print(f"Response Headers: {response.headers}")
        
        if response.status_code in [200, 201, 202]:
            print("\n✓ Email sent successfully via SendGrid!")
        else:
            print(f"\n❌ Unexpected status code: {response.status_code}")
            
    except Exception as e:
        print(f"\n❌ Error: {e}")

if __name__ == "__main__":
    send_test_email()
