"""Template variable documentation endpoint."""

from fastapi import APIRouter, Depends

from notify_bridge_core.providers.base import ServiceProviderType
from notify_bridge_core.providers.immich import ImmichServiceProvider  # noqa: F401 — triggers registration
from notify_bridge_core.templates.variables import registry

from ..auth.dependencies import get_current_user
from ..database.models import User

router = APIRouter(prefix="/api/template-vars", tags=["template-vars"])


@router.get("")
async def get_template_variables(
    provider_type: str | None = None,
    user: User = Depends(get_current_user),
):
    """Get available template variables, optionally filtered by provider type."""
    if provider_type:
        try:
            pt = ServiceProviderType(provider_type)
        except ValueError:
            return {"error": f"Unknown provider type: {provider_type}"}
        variables = registry.get_variables(pt)
    else:
        variables = registry.get_base_variables()

    return [
        {
            "name": v.name,
            "type": v.type,
            "description": v.description,
            "example": v.example,
            "provider_type": v.provider_type.value if v.provider_type else None,
        }
        for v in variables
    ]
