"""Template variable system — definitions and registry."""

from __future__ import annotations

from dataclasses import dataclass, field

from notify_bridge_core.providers.base import ServiceProviderType


@dataclass
class TemplateVariableDefinition:
    """Definition of a variable available in notification templates."""

    name: str
    type: str  # "string", "int", "list", "datetime", "bool"
    description: str
    example: str
    provider_type: ServiceProviderType | None = None  # None = base variable


# Base variables available to all providers
BASE_VARIABLES: list[TemplateVariableDefinition] = [
    TemplateVariableDefinition(
        name="event_type",
        type="string",
        description="Event type identifier (e.g., 'assets_added')",
        example="assets_added",
    ),
    TemplateVariableDefinition(
        name="timestamp",
        type="datetime",
        description="When the event occurred",
        example="2026-03-19 14:30:00",
    ),
    TemplateVariableDefinition(
        name="service_name",
        type="string",
        description="Name of the service provider instance",
        example="My Immich Server",
    ),
    TemplateVariableDefinition(
        name="service_type",
        type="string",
        description="Type of service provider",
        example="immich",
    ),
    TemplateVariableDefinition(
        name="collection_name",
        type="string",
        description="Name of the collection (album, folder, etc.)",
        example="Vacation 2026",
    ),
    TemplateVariableDefinition(
        name="collection_id",
        type="string",
        description="Unique identifier of the collection",
        example="abc-123-def",
    ),
    TemplateVariableDefinition(
        name="added_count",
        type="int",
        description="Number of assets added",
        example="5",
    ),
    TemplateVariableDefinition(
        name="removed_count",
        type="int",
        description="Number of assets removed",
        example="2",
    ),
    TemplateVariableDefinition(
        name="assets",
        type="list",
        description="List of added media assets with details",
        example="[{id, type, filename, ...}]",
    ),
    TemplateVariableDefinition(
        name="old_name",
        type="string",
        description="Previous collection name (for rename events)",
        example="Old Album Name",
    ),
    TemplateVariableDefinition(
        name="new_name",
        type="string",
        description="New collection name (for rename events)",
        example="New Album Name",
    ),
]


@dataclass
class VariableRegistry:
    """Registry of template variables, organized by provider type.

    Maintains base variables (available to all providers) and
    provider-specific variables. Queries return the union of both.
    """

    _provider_vars: dict[ServiceProviderType, list[TemplateVariableDefinition]] = field(
        default_factory=dict
    )

    def register_provider_variables(
        self,
        provider_type: ServiceProviderType,
        variables: list[TemplateVariableDefinition],
    ) -> None:
        """Register variables for a specific provider type."""
        self._provider_vars[provider_type] = variables

    def get_variables(
        self, provider_type: ServiceProviderType
    ) -> list[TemplateVariableDefinition]:
        """Get all variables available for a provider type (base + provider-specific)."""
        provider_vars = self._provider_vars.get(provider_type, [])
        return BASE_VARIABLES + provider_vars

    def get_base_variables(self) -> list[TemplateVariableDefinition]:
        """Get base variables available to all providers."""
        return list(BASE_VARIABLES)

    def get_provider_specific_variables(
        self, provider_type: ServiceProviderType
    ) -> list[TemplateVariableDefinition]:
        """Get only the provider-specific variables (excluding base)."""
        return list(self._provider_vars.get(provider_type, []))

    def get_variable_names(self, provider_type: ServiceProviderType) -> set[str]:
        """Get the set of all variable names available for a provider type."""
        return {v.name for v in self.get_variables(provider_type)}


# Global registry instance
registry = VariableRegistry()
