from __future__ import annotations

from services.base.service_interface import (
    BaseServiceProvider,
    ServiceRegistry,
    register_service,
    register_service_instance,
)


class _DummyService(BaseServiceProvider):
    def __init__(self, name: str = "dummy") -> None:
        super().__init__(name)

    async def initialize(self, **_: object) -> bool:
        self._set_initialized(True)
        return True

    async def close(self) -> bool:
        self._set_initialized(False)
        return True

    async def health_check(self) -> dict[str, object]:
        return {"status": "healthy", "details": {}}


def test_register_service_and_create_service() -> None:
    @register_service("dummy-service")
    class _RegisteredService(_DummyService):
        pass

    created = ServiceRegistry.create_service("dummy-service")

    assert created is not None
    assert created.get_service_info()["name"] == "dummy"


def test_register_service_instance_returns_same_instance() -> None:
    instance = _DummyService("singleton")

    returned = register_service_instance("dummy-singleton", instance)
    created = ServiceRegistry.create_service("dummy-singleton")

    assert returned is instance
    assert created is instance


def test_create_service_returns_none_for_missing() -> None:
    assert ServiceRegistry.create_service("missing-service") is None
