import json
import datetime

class DeadLetterQueue:
    def __init__(self, filepath):
        self.filepath = filepath

    def enqueue(self, sender_email, recipient_email, subject, body, error_message):
        timestamp = datetime.datetime.now().isoformat()
        failed_email_data = {
            'timestamp': timestamp,
            'sender_email': sender_email,
            'recipient_email': recipient_email,
            'subject': subject,
            'body': body,
            'error_message': error_message
        }

        try:
            with open(self.filepath, 'a') as f:
                f.write(json.dumps(failed_email_data) + '\n')
            return True
        except Exception as e:
            print(f"Error writing to DLQ: {e}") # Log this properly in production
            return False

    def peek(self):
        # Allows an admin to view the contents.  This is a basic implementation.
        try:
            with open(self.filepath, 'r') as f:
                return f.readlines()
        except FileNotFoundError:
            return []
        except Exception as e:
            print(f"Error reading from DLQ: {e}")
            return []