#!/usr/bin/env python3
"""
Final test for email sending with existing CSV files using compression.
"""

import os
from features.export_mailer import ExportMailer
from core.config import get_config

def test_final_email_sending():
    """Test email sending with existing CSV files and compression."""
    print("Final test: Email sending with compression")
    
    # Check if CSV files exist
    main_csv = "HOTEL123.csv"
    blocks_csv = "BLOCKS-HOTEL123.csv"
    
    if not os.path.exists(main_csv):
        print(f"ERROR: {main_csv} not found!")
        return False
        
    if not os.path.exists(blocks_csv):
        print(f"ERROR: {blocks_csv} not found!")
        return False
    
    print(f"Found CSV files:")
    print(f"  {main_csv}: {os.path.getsize(main_csv) / (1024*1024):.2f}MB")
    print(f"  {blocks_csv}: {os.path.getsize(blocks_csv) / (1024*1024):.2f}MB")
    
    # Create ExportMailer instance
    export_mailer = ExportMailer()
    
    try:
        # Test compression
        print("\nTesting file compression...")
        main_compressed = export_mailer.compress_file_if_needed(main_csv)
        blocks_compressed = export_mailer.compress_file_if_needed(blocks_csv)
        
        print(f"Main file: {main_csv} -> {main_compressed}")
        if main_compressed != main_csv:
            print(f"  Size: {os.path.getsize(main_compressed) / (1024*1024):.2f}MB")
        
        print(f"Blocks file: {blocks_csv} -> {blocks_compressed}")
        if blocks_compressed != blocks_csv:
            print(f"  Size: {os.path.getsize(blocks_compressed) / (1024*1024):.2f}MB")
        
        # Test sending emails with compressed files
        print("\nTesting email sending...")
        config = get_config()
        hotel_key = config.get("hotel_key")
        if not hotel_key:
            hotel_key = "HOTEL123"  # Fallback
        print(f"Using hotel key: {hotel_key}")
        
        success = export_mailer.send_emails(main_compressed, blocks_compressed, hotel_key)
        
        if success:
            print("✓ Both emails sent successfully!")
        else:
            print("✗ Email sending failed!")
        
        # Clean up compressed files
        if main_compressed != main_csv and os.path.exists(main_compressed):
            os.remove(main_compressed)
            print(f"Cleaned up: {main_compressed}")
            
        if blocks_compressed != blocks_csv and os.path.exists(blocks_compressed):
            os.remove(blocks_compressed)
            print(f"Cleaned up: {blocks_compressed}")
        
        return success
        
    finally:
        export_mailer.cleanup()

if __name__ == "__main__":
    test_final_email_sending()