"""Action type registry.

Defines what action types each provider supports. Used by the API to expose
available actions and validate action configurations.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass(frozen=True)
class ActionTypeDefinition:
    """Describes one type of action a provider supports."""

    key: str  # e.g. "auto_organize"
    provider_type: str  # e.g. "immich"
    display_name: str  # e.g. "Auto-Organize"
    description: str
    rule_schema: dict[str, Any] = field(default_factory=dict)
    config_schema: dict[str, Any] = field(default_factory=dict)


# ---------------------------------------------------------------------------
# Registry
# ---------------------------------------------------------------------------

_ACTION_TYPE_REGISTRY: dict[str, list[ActionTypeDefinition]] = {}


def register_action_type(defn: ActionTypeDefinition) -> None:
    """Register an action type for a provider."""
    _ACTION_TYPE_REGISTRY.setdefault(defn.provider_type, []).append(defn)


def get_action_types(provider_type: str) -> list[ActionTypeDefinition]:
    """Get all action types for a provider."""
    return list(_ACTION_TYPE_REGISTRY.get(provider_type, []))


def get_action_type(provider_type: str, key: str) -> ActionTypeDefinition | None:
    """Get a specific action type by provider and key."""
    for defn in _ACTION_TYPE_REGISTRY.get(provider_type, []):
        if defn.key == key:
            return defn
    return None


def get_all_action_types() -> dict[str, list[ActionTypeDefinition]]:
    """Get all registered action types grouped by provider."""
    return {k: list(v) for k, v in _ACTION_TYPE_REGISTRY.items()}


# ---------------------------------------------------------------------------
# Immich action types
# ---------------------------------------------------------------------------

IMMICH_AUTO_ORGANIZE = ActionTypeDefinition(
    key="auto_organize",
    provider_type="immich",
    display_name="Auto-Organize",
    description="Sort assets into albums by person, search query, date, or favorites",
    rule_schema={
        "type": "object",
        "properties": {
            "criteria": {
                "type": "object",
                "properties": {
                    "person_ids": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "Immich person UUIDs",
                    },
                    "person_names": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "Display names (UI only)",
                    },
                    "exclude_person_ids": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "Immich person UUIDs — assets with these persons are excluded",
                    },
                    "exclude_person_names": {
                        "type": "array",
                        "items": {"type": "string"},
                        "description": "Display names for excluded persons (UI only)",
                    },
                    "query": {
                        "type": "string",
                        "description": "Smart search query (CLIP)",
                    },
                    "asset_type": {
                        "type": "string",
                        "enum": ["all", "image", "video"],
                        "default": "all",
                    },
                    "date_from": {
                        "type": "string",
                        "format": "date",
                        "description": "ISO date lower bound",
                    },
                    "date_to": {
                        "type": "string",
                        "format": "date",
                        "description": "ISO date upper bound",
                    },
                    "favorite_only": {
                        "type": "boolean",
                        "default": False,
                    },
                },
            },
            "target_album_id": {
                "type": "string",
                "description": "Immich album UUID",
            },
            "target_album_name": {
                "type": "string",
                "description": "Display name (UI only)",
            },
            "create_album_if_missing": {
                "type": "boolean",
                "default": False,
            },
            "create_album_name": {
                "type": "string",
                "description": "Name for auto-created album",
            },
        },
        "required": ["criteria", "target_album_id"],
    },
)

register_action_type(IMMICH_AUTO_ORGANIZE)
