from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any
from uuid import UUID


@dataclass
class ConnectorContext:
    connector_id: UUID
    pharmacy_id: UUID
    erp_system_id: UUID
    connector_type: str
    config: dict[str, Any] = field(default_factory=dict)
    secrets: dict[str, str] = field(default_factory=dict)

    _cache_prefix: str = field(default="connector", init=False, repr=False)

    @property
    def cache_key_prefix(self) -> str:
        return f"{self._cache_prefix}:{self.connector_type}:{self.pharmacy_id}"

    def cache_key(self, suffix: str) -> str:
        return f"{self.cache_key_prefix}:{suffix}"

    def get_config(self, key: str, default: Any = None) -> Any:
        return self.config.get(key, default)

    def get_secret(self, key: str) -> str | None:
        return self.secrets.get(key)

    def require_secret(self, key: str) -> str:
        value = self.secrets.get(key)
        if value is None:
            raise ValueError(f"Required secret '{key}' not found in connector context")
        return value

    def require_config(self, key: str) -> Any:
        value = self.config.get(key)
        if value is None:
            raise ValueError(f"Required config '{key}' not found in connector context")
        return value
