#!/usr/bin/env python3
"""
Test script for email sending functionality.
"""

import os
import sys
from core.mailer import create_mailer
from core.config import get_config

def test_email_sending():
    """Test email sending with current configuration."""
    print("Testing email sending functionality...")
    
    # Get configuration
    config = get_config()
    
    # Print current email settings
    print("\nCurrent email configuration:")
    print(f"  From: {config.get('sendfrom')}")
    print(f"  To: {config.get('sendto')}")
    print(f"  Server: {config.get('mailserver')}")
    print(f"  Port: {config.get_int('port')}")
    print(f"  SSL: {config.get_int('ssl')}")
    pwd = config.get('pwd')
    print(f"  Password: {'*' * len(pwd) if pwd else 'NOT SET'}")
    
    # Debug: check raw config values
    print("\nDebug - Raw config values:")
    print(f"  pwd raw: {repr(config.config.get('settings', 'pwd', fallback=None))}")
    print(f"  pwd exists in config: {'pwd' in config.config['settings']}")
    print(f"  pwd from get(): {repr(pwd)}")
    
    # Check if CSV files exist
    main_csv = "HOTEL123.csv"
    blocks_csv = "BLOCKS-HOTEL123.csv"
    
    print(f"\nChecking CSV files:")
    print(f"  {main_csv}: {'EXISTS' if os.path.exists(main_csv) else 'MISSING'}")
    print(f"  {blocks_csv}: {'EXISTS' if os.path.exists(blocks_csv) else 'MISSING'}")
    
    if not os.path.exists(main_csv):
        print(f"\nERROR: {main_csv} not found!")
        return False
    
    # Create mailer and test sending
    mailer = create_mailer()
    
    print(f"\nTesting email sending with {main_csv}...")
    
    # Test with small file first
    small_file = "test_small.csv"
    print(f"\nTesting with small file: {small_file}")
    
    from core.mailer import send_mail
    success = send_mail(
        sendfrom=config.get('sendfrom'),
        sendto=config.get('sendto'),
        subject="TEST: Small File",
        body="TEST EMAIL - PLEASE IGNORE",
        attachment_path=small_file,
        pwd=config.get('pwd'),
        mailserver=config.get('mailserver'),
        port=config.get_int('port'),
        ssl=config.get_int('ssl')
    )
    
    if success:
        print("✓ Email with small file sent successfully!")
        
        # Now test with large file to show size issue
        print(f"\nTesting with large file: {main_csv}")
        success_large = send_mail(
            sendfrom=config.get('sendfrom'),
            sendto=config.get('sendto'),
            subject="TEST: HOTEL123",
            body="TEST EMAIL - PLEASE IGNORE",
            attachment_path=main_csv,
            pwd=config.get('pwd'),
            mailserver=config.get('mailserver'),
            port=config.get_int('port'),
            ssl=config.get_int('ssl')
        )
        
        if success_large:
            print("✓ Email with large file sent successfully!")
            return True
        else:
            print("✗ Email with large file failed (likely due to size)")
            return False
    else:
        print("✗ Email with small file failed!")
        return False
    
    if success:
        print("✓ Email sent successfully!")
    else:
        print("✗ Email sending failed!")
        print("Check log/hotellabexport.log for detailed error information")
    
    return success

if __name__ == "__main__":
    test_email_sending()