"""CLI configuration management."""

import logging
from functools import cache

import httpx
from msgspec import Struct
from slices_clientlib_core.base.config import ApiEntry, cache_dir_location, load_config_from_url_or_cache

from slices_clientlib_aiis.base.const import (
    DEFAULT_SLICES_AI_CONFIG_URL,
    SLICES_AI_CONFIG_CACHE_FILENAME,
    SLICES_AI_CONFIG_CACHE_TIMEOUT_MINUTES,
)

logger = logging.getLogger(__name__)

slices_aiis_config_cache_file_location = cache_dir_location / SLICES_AI_CONFIG_CACHE_FILENAME


class SlicesAIApiClientsConfig(Struct):
    """Configuration model used to create slices AI api client objects."""

    ai_service_default_site: str
    """Default site ID to use when not specified by user."""

    ai_service_sites: dict[str, ApiEntry]
    """AI Service site info, by slices site ID."""


@cache
def load_default_aiis_api_clients_config(
    client: httpx.Client,
    *,
    ignore_cache: bool = False,
) -> SlicesAIApiClientsConfig:
    """Load default API clients configuration for the slices AI Service.

    This is loaded from the central URL, or from a locally cached copy.
    """
    return load_config_from_url_or_cache(
        slices_aiis_config_cache_file_location,
        DEFAULT_SLICES_AI_CONFIG_URL,
        SLICES_AI_CONFIG_CACHE_TIMEOUT_MINUTES,
        SlicesAIApiClientsConfig,
        client,
        ignore_cache,
    )
