"""In-memory :class:`~mgf.cloud.manager.base.ManagerApi` — no network.

Seeded with a small, realistic sample (a free Starter hosting + its site, a
couple of DNS records) so tests, dry runs, and ``mgf-cloud manager … --provider
fake`` exercise the same surface as the real adapter without a token or a live
account. A missing resource raises the same
:class:`mgf.common.exceptions.ResourceNotFoundError` the real client does.
"""

from __future__ import annotations

from mgf.cloud.exceptions import ResourceNotFoundError
from mgf.cloud.manager.models import DnsRecord, HostingSite, Product, WebHosting

__all__ = ["FakeManager"]


def _seed_products() -> tuple[Product, ...]:
    return (
        Product(id=743284, service_name="hosting", customer_name="example.test", account_id=1),
        Product(id=2047496, service_name="domain", customer_name="example.test", account_id=1),
    )


def _seed_hostings() -> dict[int, WebHosting]:
    return {
        743284: WebHosting(
            id=743284,
            account_id=1,
            customer_name="example.test",
            version=2,
            is_free=False,
            is_zero_price=True,
        ),
    }


def _seed_sites() -> dict[int, tuple[HostingSite, ...]]:
    return {
        743284: (
            HostingSite(
                id=1349857,
                hosting_id=743284,
                main_fqdn="example.test",
                directory="/",
                hosting_type="starter",
                ssl_status="free",
                can_install_applications=True,
            ),
        ),
    }


def _seed_dns() -> dict[str, tuple[DnsRecord, ...]]:
    return {
        "example.test": (
            DnsRecord(id="1", source="", type="A", ttl=3600, target="203.0.113.10"),
            DnsRecord(id="2", source="www", type="A", ttl=3600, target="203.0.113.10"),
        ),
    }


class FakeManager:
    """An in-memory Manager-API adapter for tests and dry runs."""

    name = "fake"

    def __init__(self) -> None:
        self._products = _seed_products()
        self._hostings = _seed_hostings()
        self._sites = _seed_sites()
        self._dns = _seed_dns()

    def list_products(self) -> tuple[Product, ...]:
        return self._products

    def list_web_hostings(self) -> tuple[WebHosting, ...]:
        return tuple(self._hostings.values())

    def get_web_hosting(self, hosting_id: int) -> WebHosting:
        hosting = self._hostings.get(hosting_id)
        if hosting is None:
            raise ResourceNotFoundError(f"web hosting {hosting_id} not found")
        return hosting

    def find_web_hosting(self, fqdn: str) -> WebHosting:
        for hosting in self._hostings.values():
            if hosting.customer_name == fqdn:
                return hosting
        raise ResourceNotFoundError(f"no web hosting for {fqdn!r}")

    def list_sites(self, hosting_id: int) -> tuple[HostingSite, ...]:
        if hosting_id not in self._hostings:
            raise ResourceNotFoundError(f"web hosting {hosting_id} not found")
        return self._sites.get(hosting_id, ())

    def get_site(self, hosting_id: int, site_id: int) -> HostingSite:
        for site in self._sites.get(hosting_id, ()):
            if site.id == site_id:
                return site
        raise ResourceNotFoundError(f"site {site_id} not found under hosting {hosting_id}")

    def list_dns_records(self, fqdn: str) -> tuple[DnsRecord, ...]:
        if fqdn not in self._dns:
            raise ResourceNotFoundError(f"no DNS zone for {fqdn!r}")
        return self._dns[fqdn]
