"""Generic adapter contract for provider integrations."""
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Protocol


@dataclass(frozen=True)
class IntegrationResourceRef:
    provider_key: str
    resource_type: str
    external_resource_id: str
    external_version: str | None = None


@dataclass(frozen=True)
class IntegrationHealth:
    status: str
    message: str | None = None
    metadata: dict[str, Any] = field(default_factory=dict)


class IntegrationConnector(Protocol):
    provider_key: str
    capabilities: frozenset[str]

    async def validate_connection(self, *, config: dict[str, Any], secrets: dict[str, str]) -> IntegrationHealth: ...

    async def provision(self, *, resource_type: str, desired_state: dict[str, Any], config: dict[str, Any], secrets: dict[str, str]) -> IntegrationResourceRef: ...

    async def configure(self, *, resource: IntegrationResourceRef, desired_state: dict[str, Any], config: dict[str, Any], secrets: dict[str, str]) -> IntegrationResourceRef: ...

    async def observe(self, *, resource: IntegrationResourceRef, config: dict[str, Any], secrets: dict[str, str]) -> tuple[IntegrationHealth, dict[str, Any]]: ...

    async def disconnect(self, *, resource: IntegrationResourceRef, config: dict[str, Any], secrets: dict[str, str]) -> None: ...


class IntegrationConnectorRegistry:
    def __init__(self, connectors: list[IntegrationConnector] | None = None) -> None:
        self._connectors = {item.provider_key: item for item in connectors or []}

    def register(self, connector: IntegrationConnector) -> None:
        self._connectors[connector.provider_key] = connector

    def get(self, provider_key: str) -> IntegrationConnector | None:
        return self._connectors.get(provider_key)
