"""
HS Mail Python SDK
~~~~~~~~~~~~~~~~~~

Official Python client for the HS Mail v1 API.

    >>> from hsmail import HsMailClient
    >>> client = HsMailClient(api_key='YOUR_API_KEY')
    >>> profile = client.get_profile()
    >>> products = client.products.list()
    >>> order = client.orders.create('product-uuid')
"""
from __future__ import annotations

from ._http import HttpClient
from .resources.products import ProductResource
from .resources.orders import OrderResource
from .resources.mailbox import MailboxResource
from .resources.tools import ToolsResource
from .exceptions import (
    HsMailError,
    AuthenticationError,
    RateLimitError,
    NotFoundError,
    InsufficientBalanceError,
    ValidationError,
)

__version__ = "1.0.0"
__all__ = [
    "HsMailClient",
    "HsMailError",
    "AuthenticationError",
    "RateLimitError",
    "NotFoundError",
    "InsufficientBalanceError",
    "ValidationError",
]

DEFAULT_BASE_URL = "https://api.hsmail.shop"


class HsMailClient:
    """
    HS Mail API Client.

    Usage::

        from hsmail import HsMailClient

        client = HsMailClient(api_key="YOUR_API_KEY")

        # Ping
        client.ping()

        # Products
        client.products.list()
        client.products.get("product-uuid")

        # Orders
        client.orders.create("product-uuid", quantity=1)
        client.orders.get("ORDER_ID")

        # Mailbox
        client.mailbox.read_outlook(email, refresh_token, client_id)
        client.mailbox.check_gmail("user@gmail.com")

        # Tools
        client.tools.generate_2fa("BASE32_SECRET")
    """

    VERSION = __version__

    def __init__(
        self,
        api_key: str,
        base_url: str = DEFAULT_BASE_URL,
        timeout: int = 30,
    ):
        """
        Initialise the HS Mail client.

        Args:
            api_key:  Your HS Mail API key (from dashboard settings).
            base_url: Override the API base URL (useful for testing).
            timeout:  HTTP request timeout in seconds (default 30).
        """
        if not api_key or not api_key.strip():
            raise ValueError("api_key cannot be empty")

        http = HttpClient(api_key=api_key, base_url=base_url, timeout=timeout)

        self.products = ProductResource(http)
        self.orders   = OrderResource(http)
        self.mailbox  = MailboxResource(http)
        self.tools    = ToolsResource(http)
        self._http    = http

    def ping(self) -> dict:
        """Check API connectivity and get your public IP address."""
        return self._http.get("/v1")

    def get_profile(self) -> dict:
        """
        Get the authenticated user's profile.

        Returns:
            dict: ``{ id, name, email, balance, rankLevel, ... }``
        """
        return self._http.get("/v1/profile")
