import imaplib, email, json, re

def eden_execute():
    try:
        with open('/Eden/SECRETS/email_config.json') as f:
            config = json.load(f)
        
        mail = imaplib.IMAP4_SSL('imap-mail.outlook.com', 993)
        mail.login(config['hotmail_user'], config['hotmail_password'])
        
        # Scan every corner of the account: Junk, Archive, and Inbox
        folders = ['INBOX', 'Junk', 'Archive']
        found_link = None

        for folder in folders:
            print(f"🤖 Eden scanning {folder} for the $10,000 trigger...")
            mail.select(folder)
            _, data = mail.search(None, '(OR FROM "service@paypal.com" TEXT "confirm")')
            
            for m_id in reversed(data[0].split()):
                _, msg_data = mail.fetch(m_id, '(RFC822)')
                msg = email.message_from_bytes(msg_data[0][1])
                body = ""

                if msg.is_multipart():
                    for part in msg.walk():
                        if part.get_content_type() == "text/html":
                            body = part.get_payload(decode=True).decode()
                else:
                    body = msg.get_payload(decode=True).decode()

                # Regex to find the specific PayPal confirmation link
                links = re.findall(r'https://www.paypal.com/auth/verify\?.*?(?="|\s|>|$)', body)
                if links:
                    print(f"\n✅ EDEN SUCCESS: Found the Claim Link in {folder}!")
                    print(f"SUBJECT: {msg['Subject']}")
                    print(f"LINK: {links[0]}")
                    found_link = links[0]
                    break
            if found_link: break
        
        mail.logout()
        return found_link
    except Exception as e:
        print(f"❌ Eden hit a wall: {str(e)}")

if __name__ == "__main__":
    eden_execute()
