#!/usr/bin/env python3
import imaplib
import ssl

def test_connection():
    try:
        # Test SSL connection
        print("Testing SSL connection to localhost:9930...")
        context = ssl.create_default_context()
        context.check_hostname = False
        context.verify_mode = ssl.CERT_NONE
        
        mail = imaplib.IMAP4_SSL('localhost', 9930, ssl_context=context)
        print("SSL connection successful!")
        
        # Check capabilities
        caps = mail.capability()
        print(f"Server capabilities: {caps}")
        
        # Try login
        print("Attempting login...")
        result = mail.login('tanya@jongsma.me', 'Tanya-Migrate-2026!')
        print(f"Login result: {result}")
        
        # List mailboxes
        print("Listing mailboxes...")
        status, mailboxes = mail.list()
        for box in mailboxes:
            print(f"  {box}")
        
        mail.logout()
        print("Test successful!")
        
    except Exception as e:
        print(f"Connection failed: {e}")
        
        # Try without SSL
        print("\nTrying without SSL...")
        try:
            mail = imaplib.IMAP4('localhost', 9930)
            caps = mail.capability()
            print(f"Plain connection capabilities: {caps}")
            mail.logout()
        except Exception as e2:
            print(f"Plain connection also failed: {e2}")

if __name__ == "__main__":
    test_connection()