import imaplib, email, json, sys

def hunt_whale():
    try:
        with open('/Eden/SECRETS/email_config.json') as f:
            config = json.load(f)
        
        print("📡 Establishing Secure Bridge to Hotmail...")
        mail = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
        mail.login(config['hotmail_user'], config['hotmail_password'])
        
        # Pull all folder names to ensure we don't miss 'Junk' or 'Spam'
        _, folders = mail.list()
        
        for folder_info in folders:
            folder_name = folder_info.decode().split(' "/" ')[-1].strip('"')
            print(f"🔎 Scanning folder: {folder_name}...")
            mail.select(f'"{folder_name}"')
            
            # Searching for the specific 10k or PayPal signature
            result, data = mail.search(None, '(OR TEXT "PayPal" TEXT "10,000")')
            ids = data[0].split()
            
            if ids:
                print(f"🎯 FOUND {len(ids)} items in {folder_name}. Extracting Whale signatures...")
                for m_id in ids[-3:]: # Get the most recent hits
                    _, msg_data = mail.fetch(m_id, '(RFC822)')
                    msg = email.message_from_bytes(msg_data[0][1])
                    print(f"\n--- MATCH IN [{folder_name}] ---")
                    print(f"DATE:    {msg['Date']}")
                    print(f"SUBJECT: {msg['Subject']}")
                    print(f"FROM:    {msg['From']}")
                    print("-" * 30)

        mail.logout()
    except Exception as e:
        print(f"❌ Connection Error: {str(e)}")

if __name__ == "__main__":
    hunt_whale()
