"""Load default command templates from .jinja2 files."""

import logging
from pathlib import Path

_LOGGER = logging.getLogger(__name__)

_DEFAULTS_DIR = Path(__file__).parent

# Per-provider slot names
PROVIDER_COMMAND_SLOTS: dict[str, list[str]] = {
    "immich": [
        # Response templates
        "start", "help", "status", "albums", "events", "people",
        "search", "latest", "favorites", "random", "summary", "memory",
        "rate_limited", "no_results",
        # Description slots
        "desc_status", "desc_albums", "desc_events", "desc_summary",
        "desc_latest", "desc_memory", "desc_random", "desc_search",
        "desc_find", "desc_person", "desc_place", "desc_favorites",
        "desc_people", "desc_help",
        # Usage example slots
        "usage_search", "usage_find", "usage_person", "usage_place",
        "usage_latest", "usage_random", "usage_favorites", "usage_events",
        "usage_memory",
    ],
    "gitea": [
        # Response templates
        "start", "help", "status", "repos", "issues", "prs", "commits",
        "rate_limited", "no_results",
        # Description slots
        "desc_help", "desc_status", "desc_repos", "desc_issues",
        "desc_prs", "desc_commits",
    ],
    "planka": [
        # Response templates
        "start", "help", "status", "boards", "cards", "lists",
        "rate_limited", "no_results",
        # Description slots
        "desc_help", "desc_status", "desc_boards", "desc_cards",
        "desc_lists",
    ],
    "nut": [
        # Response templates
        "start", "help", "status", "devices", "battery",
        "rate_limited", "no_results",
        # Description slots
        "desc_help", "desc_status", "desc_devices", "desc_battery",
    ],
    "webhook": [
        # Response templates
        "start", "help", "status",
        "rate_limited", "no_results",
        # Description slots
        "desc_help", "desc_status",
    ],
    "google_photos": [
        # Response templates
        "start", "help", "status", "albums", "latest", "search", "random",
        "rate_limited", "no_results",
        # Description slots
        "desc_help", "desc_status", "desc_albums",
        "desc_latest", "desc_search", "desc_random",
        # Usage example slots
        "usage_latest", "usage_search", "usage_random",
    ],
    "home_assistant": [
        # Response templates
        "start", "help", "status", "entities", "state", "areas",
        "rate_limited", "no_results",
        # Description slots
        "desc_help", "desc_status", "desc_entities", "desc_state", "desc_areas",
        # Usage examples
        "usage_entities", "usage_state",
    ],
    "bridge_self": [
        # Response templates
        "start", "help", "status", "thresholds", "reset", "health",
        "rate_limited", "no_results",
        # Description slots
        "desc_help", "desc_status", "desc_thresholds", "desc_reset", "desc_health",
        # Usage examples
        "usage_reset",
    ],
}

# Backward-compatible aliases
COMMAND_SLOT_NAMES = [
    "start", "help", "status", "albums", "events", "people",
    "search", "latest", "favorites", "random", "summary", "memory",
    "rate_limited", "no_results",
]
COMMAND_DESC_SLOT_NAMES = [
    "desc_status", "desc_albums", "desc_events", "desc_summary",
    "desc_latest", "desc_memory", "desc_random", "desc_search",
    "desc_find", "desc_person", "desc_place", "desc_favorites",
    "desc_people", "desc_help",
]
ALL_SLOT_NAMES = COMMAND_SLOT_NAMES + COMMAND_DESC_SLOT_NAMES


def load_default_command_templates(
    locale: str = "en",
    provider_type: str = "immich",
) -> dict[str, str]:
    """Load default command template strings for a locale and provider type.

    For "immich", templates are in {locale}/ (root, backward compat).
    For other providers, templates are in {locale}/{provider_type}/.

    Returns dict mapping slot_name -> template string.
    """
    if provider_type == "immich":
        locale_dir = _DEFAULTS_DIR / locale
    else:
        locale_dir = _DEFAULTS_DIR / locale / provider_type

    if not locale_dir.is_dir():
        _LOGGER.warning("No default command templates for locale '%s' provider '%s'", locale, provider_type)
        return {}

    slot_names = PROVIDER_COMMAND_SLOTS.get(provider_type, ALL_SLOT_NAMES)
    templates: dict[str, str] = {}
    for slot_name in slot_names:
        filepath = locale_dir / f"{slot_name}.jinja2"
        if filepath.exists():
            templates[slot_name] = filepath.read_text(encoding="utf-8").strip()
        else:
            _LOGGER.debug("Missing default command template: %s/%s.jinja2", locale_dir.name, slot_name)

    return templates
