"""Disposable inbox service."""

from typing import Any, Dict
from sed_sh.services.base import BaseService


class InboxService(BaseService):
    """Service for managing disposable email inboxes.

    All methods may raise SedSHError or its subclasses on error.
    """

    def create(self) -> Dict[str, Any]:
        """Create a new disposable inbox.

        Returns:
            Dict containing:
                - code (str): Inbox identifier code
                - email (str): The disposable email address
                - expiresAt (int): Unix timestamp when inbox expires
                - messageCount (int): Current message count (0 for new inboxes)

        Raises:
            AuthenticationError: If API key is invalid
            SedSHError: On other errors

        Example:
            >>> inbox = client.inbox.create()
            >>> print(f"Email: {inbox['email']}")
            >>> print(f"Code: {inbox['code']}")
        """
        return self._request("POST", "/inbox", json={})

    def list(self) -> Dict[str, Any]:
        """List all inboxes for the current API key.

        Returns:
            Dict containing:
                - inboxes (list): List of inbox objects, each containing:
                    - code (str): Inbox identifier
                    - email (str): Email address
                    - messageCount (int): Number of messages
                    - expiresAt (int): Expiration timestamp
                    - createdAt (int): Creation timestamp

        Raises:
            AuthenticationError: If API key is invalid
            SedSHError: On other errors

        Example:
            >>> inboxes = client.inbox.list()
            >>> for inbox in inboxes['inboxes']:
            ...     print(f"{inbox['email']} - {inbox['messageCount']} messages")
        """
        return self._request("GET", "/inbox")

    def get_messages(self, inbox_code: str) -> Dict[str, Any]:
        """Get all messages for an inbox.

        Args:
            inbox_code: The inbox code

        Returns:
            Dict containing:
                - messages (list): List of message objects, each containing:
                    - messageId (str): Message identifier
                    - from (str): Sender email
                    - subject (str): Email subject
                    - receivedAt (int): Unix timestamp
                    - read (bool): Whether message has been read

        Raises:
            ResourceNotFoundError: If inbox doesn't exist
            AuthenticationError: If API key is invalid
            SedSHError: On other errors

        Example:
            >>> messages = client.inbox.get_messages("inbox_123")
            >>> for msg in messages['messages']:
            ...     print(f"From: {msg['from']}, Subject: {msg['subject']}")
        """
        return self._request("GET", f"/inbox/{inbox_code}/messages")

    def get_message(self, inbox_code: str, message_id: str) -> Dict[str, Any]:
        """Get details of a specific message.

        Args:
            inbox_code: The inbox code
            message_id: The message identifier

        Returns:
            Dict containing:
                - messageId (str): Message identifier
                - from (str): Sender email
                - to (str): Recipient email
                - subject (str): Email subject
                - body (str): Email body (plain text)
                - receivedAt (int): Unix timestamp
                - read (bool): Whether message has been read

        Raises:
            ResourceNotFoundError: If inbox or message doesn't exist
            AuthenticationError: If API key is invalid
            SedSHError: On other errors

        Example:
            >>> msg = client.inbox.get_message("inbox_123", "msg_456")
            >>> print(f"Body: {msg['body']}")
        """
        return self._request("GET", f"/inbox/{inbox_code}/messages/{message_id}")

    def delete_message(self, inbox_code: str, message_id: str) -> Dict[str, Any]:
        """Delete a specific message.

        Args:
            inbox_code: The inbox code
            message_id: The message identifier

        Returns:
            Dict containing success confirmation

        Raises:
            ResourceNotFoundError: If inbox or message doesn't exist
            AuthenticationError: If API key is invalid
            SedSHError: On other errors

        Example:
            >>> result = client.inbox.delete_message("inbox_123", "msg_456")
        """
        return self._request("DELETE", f"/inbox/{inbox_code}/messages/{message_id}")

    def delete(self, inbox_code: str) -> Dict[str, Any]:
        """Delete an inbox and all its messages.

        Args:
            inbox_code: The inbox code

        Returns:
            Dict containing success confirmation

        Raises:
            ResourceNotFoundError: If inbox doesn't exist
            AuthenticationError: If API key is invalid
            SedSHError: On other errors

        Example:
            >>> result = client.inbox.delete("inbox_123")
        """
        return self._request("DELETE", f"/inbox/{inbox_code}")
