"""Example: Using the Inbox service for disposable email addresses."""

from sed_sh import SedSH
import time

# Initialize client
client = SedSH()

# Example 1: Create a disposable inbox
print("=== Example 1: Create inbox ===")
inbox = client.inbox.create()
print(f"Email: {inbox['email']}")
print(f"Code: {inbox['code']}")
print(f"Expires at: {inbox['expiresAt']}")
print(f"Use this email to receive temporary messages")
print()

# Example 2: List all inboxes
print("=== Example 2: List all inboxes ===")
inboxes = client.inbox.list()
print(f"Total inboxes: {len(inboxes['inboxes'])}")
for ib in inboxes['inboxes']:
    print(f"  - {ib['email']}: {ib['messageCount']} messages")
print()

# Example 3: Check for messages
print("=== Example 3: Check for messages ===")
print("Send an email to the inbox above, then run this code...")
print(f"Inbox code: {inbox['code']}")

# Wait a moment for messages (in real usage, you'd poll periodically)
print("Checking for messages...")
messages = client.inbox.get_messages(inbox['code'])
print(f"Messages received: {len(messages['messages'])}")

if messages['messages']:
    # Get the first message
    msg = messages['messages'][0]
    print(f"\nFirst message:")
    print(f"  From: {msg['from']}")
    print(f"  Subject: {msg['subject']}")
    print(f"  Received: {msg['receivedAt']}")

    # Get full message details
    full_msg = client.inbox.get_message(inbox['code'], msg['messageId'])
    print(f"  Body: {full_msg['body'][:100]}...")  # First 100 chars

    # Delete the message
    client.inbox.delete_message(inbox['code'], msg['messageId'])
    print(f"  Deleted message: {msg['messageId']}")
else:
    print("No messages yet. Send an email to the inbox and try again.")
print()

# Example 4: Delete an inbox
print("=== Example 4: Delete inbox ===")
# Create a temporary inbox
temp_inbox = client.inbox.create()
print(f"Created temporary inbox: {temp_inbox['email']}")

# Delete it
result = client.inbox.delete(temp_inbox['code'])
print(f"Deleted inbox: {result}")
print()

# Example 5: Using for automated testing
print("=== Example 5: Automated email testing ===")
print("Common use case: Testing email notifications in your app")
print()
print("# Step 1: Create test inbox")
test_inbox = client.inbox.create()
print(f"test_email = '{test_inbox['email']}'")
print()
print("# Step 2: Trigger email in your app (e.g., registration, password reset)")
print("# your_app.send_verification_email(test_email)")
print()
print("# Step 3: Poll for the email")
print("import time")
print("for i in range(10):  # Try for 10 seconds")
print("    messages = client.inbox.get_messages(test_inbox['code'])")
print("    if messages['messages']:")
print("        msg = client.inbox.get_message(")
print("            test_inbox['code'],")
print("            messages['messages'][0]['messageId']")
print("        )")
print("        # Extract verification link from email body")
print("        verification_link = extract_link(msg['body'])")
print("        print(f'Found verification link: {verification_link}')")
print("        break")
print("    time.sleep(1)")
print()

# Clean up - delete the inbox we created in Example 1
print("=== Cleanup ===")
client.inbox.delete(inbox['code'])
print(f"Deleted inbox: {inbox['email']}")
