#!/usr/bin/env python
"""
Test script to verify email sending via simulator API
"""
import os
import sys
import django

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.dev')
django.setup()

from apps.notifications.services import NotificationService

print("=" * 70)
print("Testing Email Sending via Simulator API")
print("=" * 70)

# Test 1: Send plain text email
print("\n1. Testing plain text email...")
try:
    log = NotificationService.send_email(
        email='test@example.com',
        subject='Test Email - Plain Text',
        message='This is a test email sent via simulator API.'
    )
    if log and log.status == 'sent':
        print(f"✅ Plain text email sent successfully!")
        print(f"   Log ID: {log.id}")
        print(f"   Recipient: {log.recipient}")
    else:
        print(f"❌ Failed to send plain text email")
        print(f"   Status: {log.status if log else 'No log created'}")
except Exception as e:
    print(f"❌ Error: {str(e)}")

# Test 2: Send HTML email
print("\n2. Testing HTML email...")
try:
    html_message = """
    <html>
    <body>
        <h1>Test HTML Email</h1>
        <p>This is a <strong>test</strong> email with <em>HTML</em> formatting.</p>
        <p>It should display nicely in the simulator.</p>
    </body>
    </html>
    """
    log = NotificationService.send_email(
        email='test@example.com',
        subject='Test Email - HTML',
        message='Plain text version of HTML email',
        html_message=html_message
    )
    if log and log.status == 'sent':
        print(f"✅ HTML email sent successfully!")
        print(f"   Log ID: {log.id}")
        print(f"   Recipient: {log.recipient}")
    else:
        print(f"❌ Failed to send HTML email")
        print(f"   Status: {log.status if log else 'No log created'}")
except Exception as e:
    print(f"❌ Error: {str(e)}")

# Test 3: Test SMS sending
print("\n3. Testing SMS sending...")
try:
    log = NotificationService.send_sms(
        phone='+966501234567',
        message='This is a test SMS sent via simulator API.'
    )
    if log and log.status == 'sent':
        print(f"✅ SMS sent successfully!")
        print(f"   Log ID: {log.id}")
        print(f"   Recipient: {log.recipient}")
    else:
        print(f"❌ Failed to send SMS")
        print(f"   Status: {log.status if log else 'No log created'}")
except Exception as e:
    print(f"❌ Error: {str(e)}")

print("\n" + "=" * 70)
print("Test Complete!")
print("=" * 70)
print("\nCheck your terminal where the server is running to see formatted output.")
print("Check logs/px360.log for detailed logs.")
