"""The Infomaniak Manager-API subsystem — hosting + DNS reads.

Separate from the OpenStack Public Cloud that :func:`mgf.cloud.get_provider`
drives: the Manager API governs the *account's* products (web hosting, domains,
DNS, mail), and authenticates with a Bearer token (see
:class:`~mgf.cloud.manager.config.ManagerCredentials`), not OpenStack
credentials.

:func:`get_manager` returns a :class:`~mgf.cloud.manager.base.ManagerApi`. The
``fake`` adapter (in-memory, no deps) is always available; ``infomaniak`` loads
lazily. The whole subsystem is standard-library-only (no HTTP SDK), so importing
it stays light.
"""

from __future__ import annotations

from mgf.cloud.exceptions import CredentialsError, ProviderNotFoundError
from mgf.cloud.manager.base import ManagerApi
from mgf.cloud.manager.config import (
    DEFAULT_BASE_URL,
    ManagerCredentials,
    load_manager_credentials,
)
from mgf.cloud.manager.models import DnsRecord, HostingSite, Product, WebHosting

__all__ = [
    "AVAILABLE_MANAGERS",
    "DEFAULT_BASE_URL",
    "DnsRecord",
    "HostingSite",
    "ManagerApi",
    "ManagerCredentials",
    "Product",
    "WebHosting",
    "available_managers",
    "get_manager",
    "load_manager_credentials",
]

AVAILABLE_MANAGERS: tuple[str, ...] = ("fake", "infomaniak")


def available_managers() -> tuple[str, ...]:
    """The manager names :func:`get_manager` accepts."""
    return AVAILABLE_MANAGERS


def get_manager(
    name: str = "infomaniak", credentials: ManagerCredentials | None = None
) -> ManagerApi:
    """Return the Manager-API adapter for ``name``.

    ``fake`` needs no credentials; ``infomaniak`` requires a
    :class:`ManagerCredentials`. Raises :class:`ProviderNotFoundError` for an
    unknown name and :class:`CredentialsError` if the real adapter has no token.
    """
    if name == "fake":
        from mgf.cloud.manager.fake import FakeManager

        return FakeManager()
    if name == "infomaniak":
        if credentials is None:
            raise CredentialsError(
                "the 'infomaniak' manager requires credentials", missing=("token",)
            )
        from mgf.cloud.manager.infomaniak import InfomaniakManager

        return InfomaniakManager(credentials)
    raise ProviderNotFoundError(name, AVAILABLE_MANAGERS)
