"""Base OpenAI-compatible endpoint provider for Local Deep Research."""

from langchain_openai import ChatOpenAI
from loguru import logger

from ...config.thread_settings import (
    get_setting_from_snapshot as _get_setting_from_snapshot,
    NoSettingsContextError,
)
from ...utilities.url_utils import normalize_url


def get_setting_from_snapshot(
    key, default=None, username=None, settings_snapshot=None
):
    """Get setting from context only - no database access from threads.

    This is a wrapper around the shared function that enables fallback LLM check.
    """
    return _get_setting_from_snapshot(
        key, default, username, settings_snapshot, check_fallback_llm=True
    )


class OpenAICompatibleProvider:
    """Base class for OpenAI-compatible API providers.

    This class provides a common implementation for any service that offers
    an OpenAI-compatible API endpoint (Google, OpenRouter, Groq, Together, etc.)
    """

    # Override these in subclasses
    provider_name = "openai_endpoint"  # Name used in logs
    api_key_setting = "llm.openai_endpoint.api_key"  # Settings key for API key
    default_base_url = "https://api.openai.com/v1"  # Default endpoint URL
    default_model = "gpt-3.5-turbo"  # Default model if none specified

    @classmethod
    def create_llm(cls, model_name=None, temperature=0.7, **kwargs):
        """Factory function for OpenAI-compatible LLMs.

        Args:
            model_name: Name of the model to use
            temperature: Model temperature (0.0-1.0)
            **kwargs: Additional arguments including settings_snapshot

        Returns:
            A configured ChatOpenAI instance

        Raises:
            ValueError: If API key is not configured
        """
        settings_snapshot = kwargs.get("settings_snapshot")

        # Get API key from settings (if provider requires one)
        if cls.api_key_setting:
            api_key = get_setting_from_snapshot(
                cls.api_key_setting,
                default=None,
                settings_snapshot=settings_snapshot,
            )

            if not api_key:
                logger.error(
                    f"{cls.provider_name} API key not found in settings"
                )
                raise ValueError(
                    f"{cls.provider_name} API key not configured. "
                    f"Please set {cls.api_key_setting} in settings."
                )
        else:
            # Provider doesn't require API key (e.g., LM Studio)
            api_key = kwargs.get("api_key", "dummy-key")

        # Use default model if none specified
        if not model_name:
            model_name = cls.default_model

        # Get endpoint URL (can be overridden in kwargs for flexibility)
        base_url = kwargs.get("base_url", cls.default_base_url)
        base_url = normalize_url(base_url) if base_url else cls.default_base_url

        # Build parameters for OpenAI client
        llm_params = {
            "model": model_name,
            "api_key": api_key,
            "base_url": base_url,
            "temperature": temperature,
        }

        # Add max_tokens if specified in settings
        try:
            max_tokens = get_setting_from_snapshot(
                "llm.max_tokens",
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if max_tokens:
                llm_params["max_tokens"] = int(max_tokens)
        except NoSettingsContextError:
            pass  # Optional parameter

        # Add streaming if specified
        try:
            streaming = get_setting_from_snapshot(
                "llm.streaming",
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if streaming is not None:
                llm_params["streaming"] = streaming
        except NoSettingsContextError:
            pass  # Optional parameter

        # Add max_retries if specified
        try:
            max_retries = get_setting_from_snapshot(
                "llm.max_retries",
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if max_retries is not None:
                llm_params["max_retries"] = max_retries
        except NoSettingsContextError:
            pass  # Optional parameter

        # Add request_timeout if specified
        try:
            request_timeout = get_setting_from_snapshot(
                "llm.request_timeout",
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if request_timeout is not None:
                llm_params["request_timeout"] = request_timeout
        except NoSettingsContextError:
            pass  # Optional parameter

        logger.info(
            f"Creating {cls.provider_name} LLM with model: {model_name}, "
            f"temperature: {temperature}, endpoint: {base_url}"
        )

        return ChatOpenAI(**llm_params)

    @classmethod
    def _create_llm_instance(cls, model_name=None, temperature=0.7, **kwargs):
        """Internal method to create LLM instance with provided parameters.

        This bypasses API key checking for providers that handle auth differently.
        """
        settings_snapshot = kwargs.get("settings_snapshot")

        # Use default model if none specified
        if not model_name:
            model_name = cls.default_model

        # Get endpoint URL (can be overridden in kwargs for flexibility)
        base_url = kwargs.get("base_url", cls.default_base_url)
        base_url = normalize_url(base_url) if base_url else cls.default_base_url

        # Get API key from kwargs (caller is responsible for providing it)
        api_key = kwargs.get("api_key", "dummy-key")

        # Build parameters for OpenAI client
        llm_params = {
            "model": model_name,
            "api_key": api_key,
            "base_url": base_url,
            "temperature": temperature,
        }

        # Add optional parameters (same as in create_llm)
        try:
            max_tokens = get_setting_from_snapshot(
                "llm.max_tokens",
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if max_tokens:
                llm_params["max_tokens"] = int(max_tokens)
        except NoSettingsContextError:
            pass

        return ChatOpenAI(**llm_params)

    @classmethod
    def is_available(cls, settings_snapshot=None):
        """Check if this provider is available.

        Args:
            settings_snapshot: Optional settings snapshot to use

        Returns:
            True if API key is configured (or not needed), False otherwise
        """
        try:
            # If provider doesn't require API key, it's available
            if not cls.api_key_setting:
                return True

            # Check if API key is configured
            api_key = get_setting_from_snapshot(
                cls.api_key_setting,
                default=None,
                settings_snapshot=settings_snapshot,
            )
            return bool(api_key)
        except Exception:
            return False

    @classmethod
    def requires_auth_for_models(cls):
        """Check if this provider requires authentication for listing models.

        Override in subclasses that don't require auth.

        Returns:
            True if authentication is required, False otherwise
        """
        return True

    @classmethod
    def list_models_for_api(cls, api_key=None, settings_snapshot=None):
        """List available models for API endpoint use.

        This method is designed to be called from Flask routes.

        Args:
            api_key: Optional API key (if None and required, returns empty list)
            settings_snapshot: Optional settings snapshot for provider-specific configuration

        Returns:
            List of model dictionaries with 'value' and 'label' keys
        """
        try:
            # Check if auth is required
            if cls.requires_auth_for_models():
                if not api_key:
                    logger.debug(
                        f"{cls.provider_name} requires API key for model listing"
                    )
                    return []
            else:
                # Use a dummy key for providers that don't require auth
                api_key = api_key or "dummy-key-for-models-list"

            from openai import OpenAI

            # Create OpenAI client
            client = OpenAI(api_key=api_key, base_url=cls.default_base_url)

            # Fetch models
            logger.debug(
                f"Fetching models from {cls.provider_name} at {cls.default_base_url}"
            )
            models_response = client.models.list()

            models = []
            for model in models_response.data:
                if model.id:
                    models.append(
                        {
                            "value": model.id,
                            "label": model.id,
                        }
                    )

            logger.info(f"Found {len(models)} models from {cls.provider_name}")
            return models

        except Exception as e:
            logger.exception(
                f"Error listing models from {cls.provider_name}: {e}"
            )
            return []

    @classmethod
    def list_models(cls, settings_snapshot=None):
        """List available models from this provider.

        Args:
            settings_snapshot: Optional settings snapshot to use

        Returns:
            List of model dictionaries with 'value' and 'label' keys
        """
        try:
            # Get API key from settings if auth is required
            api_key = None
            if cls.requires_auth_for_models():
                api_key = get_setting_from_snapshot(
                    cls.api_key_setting,
                    default=None,
                    settings_snapshot=settings_snapshot,
                )

            # Use the shared implementation with settings_snapshot
            return cls.list_models_for_api(api_key, settings_snapshot)

        except Exception as e:
            logger.exception(
                f"Error listing models from {cls.provider_name}: {e}"
            )
            return []
