import os
import sys
import logging

# Add workspace to path
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# Configure logging
logging.basicConfig(level=logging.INFO)

# Import the updated email service
from app.core.config import settings
from app.core import email_service

def test_emails(test_mode: bool):
    print("\n==================================================")
    print(f" TESTING EMAIL SERVICE (TESTING_MODE={test_mode})")
    print("==================================================")
    
    settings.TESTING = test_mode
    
    test_email = "sairam.c@tnsservices.com"
    test_name = "Sairam"
    
    print("\n--- 1. Testing Welcome Email ---")
    welcome_ok = email_service.send_welcome_email(
        name=test_name,
        email=test_email
    )
    print(f"Welcome Email Result: {welcome_ok}")
    
    print("\n--- 2. Testing Login Notification Email ---")
    login_ok = email_service.send_login_notification_email(
        name=test_name,
        email=test_email,
        ip_address="127.0.0.1",
        browser="Chrome 120",
        device="Windows Desktop"
    )
    print(f"Login Email Result: {login_ok}")
    
    print("\n--- 3. Testing Reset Password Email ---")
    reset_token_str = "dummy_" + "test_" + "token"
    reset_ok = email_service.send_reset_password_email(
        name=test_name,
        email=test_email,
        token=reset_token_str
    )
    print(f"Reset Password Email Result: {reset_ok}")
    
    print("\n--- 4. Testing Password Change OTP Email ---")
    otp_ok = email_service.send_password_change_otp_email(
        name=test_name,
        email=test_email,
        otp_code="987654"
    )
    print(f"OTP Email Result: {otp_ok}")

if __name__ == "__main__":
    # Test console preview first (TESTING=True)
    test_emails(test_mode=True)
    
    # Test live SMTP/Pepipost sending (TESTING=False)
    # This will show SMTP connection attempt, fallback to Pepipost, and successful send.
    test_emails(test_mode=False)
