from __future__ import annotations

from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Optional, Protocol, runtime_checkable

if TYPE_CHECKING:
    from mockmesh.core.fallback import FallbackConfig
    from mockmesh.response.engine import ResponseEngine
    from mockmesh.storage.manager import StorageManager


# ---------------------------------------------------------------------------

@runtime_checkable
class InterceptorProtocol(Protocol):

    name: str

    def activate(self, config: dict, **kwargs) -> None: ...

    def deactivate(self) -> None: ...

    def is_active(self) -> bool: ...


# ---------------------------------------------------------------------------

class BasePlugin(ABC):

    name: str = "base"

    def __init__(self, response_engine: "ResponseEngine") -> None:
        self.response_engine = response_engine
        self.storage: Optional["StorageManager"] = None
        self._active = False

    @abstractmethod
    def activate(
        self,
        config: dict,
        *,
        storage: Optional["StorageManager"] = None,
        provider_defaults: Optional[dict] = None,
        fallback_config: Optional["FallbackConfig"] = None,
        custom_handlers: Optional[dict] = None,
        **kwargs: Any,
    ) -> None: ...

    @abstractmethod
    def deactivate(self) -> None: ...

    def is_active(self) -> bool:
        return self._active

    def __repr__(self) -> str:  # pragma: no cover
        return f"<{self.__class__.__name__} name={self.name!r} active={self._active}>"
