import imaplib, email, json, sys

# Primary Target: jameyecho@hotmail.com / jamlen@hotmail.ca
try:
    with open('/Eden/SECRETS/email_config.json') as f:
        config = json.load(f)
    
    print("📡 Attempting Secure Handshake with Hotmail...")
    # Using the specific legacy outlook server which is often more stable for scripts
    mail = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
    
    # We use hotmail_user/password from your config
    mail.login(config['hotmail_user'], config['hotmail_password'])
    mail.select('inbox')

    print("🔎 Searching Hotmail for the $10,000 Whale...")
    result, data = mail.search(None, '(OR FROM "service@paypal.com" TEXT "PayPal")')
    
    ids = data[0].split()
    if not ids:
        print("❌ No PayPal receipts found in Hotmail inbox.")
        sys.exit()

    print(f"✅ SUCCESS! Found {len(ids)} PayPal records. Extracting latest...")
    
    for m_id in ids[-3:]:
        _, msg_data = mail.fetch(m_id, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                msg = email.message_from_bytes(response_part[1])
                print("\n" + "🐋 " + "="*40)
                print(f"DATE:    {msg['Date']}")
                print(f"TO:      {msg['To']}")
                print(f"SUBJECT: {msg['Subject']}")
                # Attempt to find a 'Claim' or 'Transaction' ID in the snippet
                print("="*42)
    
    mail.logout()
except Exception as e:
    print(f"❌ Handshake Failed: {str(e)}")
    print("\n💡 ACTION REQUIRED: If it still fails, you MUST log in to Outlook.com")
    print("   in a browser once to 'whitelist' this login attempt.")
