"""
Model list fetching base class
"""

import logging
from abc import ABC, abstractmethod
from typing import Any, List

from .log_config import LOGGER_NAMESPACE


class FetchBase(ABC):
    """Model list fetching base class with integrated logging and error handling."""

    @property
    @abstractmethod
    def provider(self) -> str:
        """Provider identifier, such as 'zhipu', 'openrouter'"""
        pass

    @abstractmethod
    def fetch_models(self) -> List[str]:
        """Fetch model list from remote"""
        pass

    @property
    def _logger(self) -> logging.Logger:
        """
        Get a logger instance with the provider name as identifier.
        
        Returns:
            Logger instance namespaced under oiiai.fetchModelList.{provider}
        """
        return logging.getLogger(f"{LOGGER_NAMESPACE}.{self.provider}")

    def _log_error(self, message: str, exc_info: bool = False) -> None:
        """
        Log an ERROR level message.
        
        Args:
            message: The error message to log.
            exc_info: Whether to include exception info (default: False).
        """
        self._logger.error(message, exc_info=exc_info)

    def _log_warning(self, message: str) -> None:
        """
        Log a WARNING level message.
        
        Args:
            message: The warning message to log.
        """
        self._logger.warning(message)

    def _log_info(self, message: str) -> None:
        """
        Log an INFO level message.
        
        Args:
            message: The info message to log.
        """
        self._logger.info(message)

    def _log_debug(self, message: str) -> None:
        """
        Log a DEBUG level message.
        
        Args:
            message: The debug message to log.
        """
        self._logger.debug(message)

    def _handle_http_error(self, error: Exception) -> List[str]:
        """
        Handle HTTP request errors by logging and returning empty list.
        
        Args:
            error: The exception that occurred during HTTP request.
            
        Returns:
            Empty list to indicate no models were fetched.
        """
        self._log_error(
            f"[{self.provider}] HTTP request failed: {error}",
            exc_info=True
        )
        return []

    def _handle_unexpected_format(self, data: Any) -> List[str]:
        """
        Handle unexpected API response format by logging and returning empty list.
        
        Args:
            data: The unexpected data received from the API.
            
        Returns:
            Empty list to indicate no models were fetched.
        """
        self._log_warning(
            f"[{self.provider}] Unexpected API response format: {type(data).__name__}"
        )
        return []

    def _handle_missing_env_var(self, var_name: str) -> List[str]:
        """
        Handle missing environment variable by logging and returning empty list.
        
        Args:
            var_name: The name of the missing environment variable.
            
        Returns:
            Empty list to indicate no models were fetched.
        """
        self._log_warning(
            f"[{self.provider}] Missing environment variable: {var_name}"
        )
        return []
