"""Shared pytest fixtures for the mgf-vm-core test suite.

Per TESTING.md: tier 1 (unit) — no infra, no daemons, no network.
Real adapter tests live in the *adapter* repos and use the
mgf.vm.core.conformance suite.
"""

from __future__ import annotations

import pytest

from mgf.vm.core.domain import NetworkInfo, VMHandle, VMStatus
from mgf.vm.core.exceptions import DomainNotFoundError


class FakeProvider:
    """A tiny pure backend fake for the creator validator's live-state rules.

    Satisfies the validator's structural ``_NameNetworkProvider`` Protocol
    by supplying exactly the two methods it calls — ``list_networks`` and
    ``get_domain`` — with the same observable contract (a free name raises
    :class:`DomainNotFoundError`).

    The original mgf-vm tests drove the validator with the adapter-bound
    ``MockProvider`` (libvirt-coupled, not importable from pure core); this
    fake replaces it. It seeds a single ``"default"`` network, mirroring
    MockProvider, so a spec whose default NIC targets ``"default"``
    validates against a fresh provider.
    """

    def __init__(self) -> None:
        self._networks: dict[str, NetworkInfo] = {
            "default": NetworkInfo(name="default", uuid="uuid-default", active=True, autostart=True)
        }
        self._domains: set[str] = set()

    def seed_network(self, info: NetworkInfo) -> None:
        self._networks[info.name] = info

    def seed_domain(self, name: str) -> None:
        self._domains.add(name)

    def list_networks(self) -> list[NetworkInfo]:
        return list(self._networks.values())

    def get_domain(self, name: str) -> VMHandle:
        if name not in self._domains:
            raise DomainNotFoundError(name)
        return VMHandle(name=name, uuid=f"uuid-{name}", state=VMStatus.STOPPED)


@pytest.fixture
def fake_provider_cls() -> type[FakeProvider]:
    """Return the :class:`FakeProvider` class for tests that construct and
    seed their own instances."""
    return FakeProvider
