"""OpenRouter LLM provider for Local Deep Research."""

from loguru import logger

from ...llm_registry import register_llm
from ..openai_base import OpenAICompatibleProvider


class OpenRouterProvider(OpenAICompatibleProvider):
    """OpenRouter provider using OpenAI-compatible endpoint.

    OpenRouter provides access to many different models through a unified
    OpenAI-compatible API, automatically supporting all current and future
    models without needing code updates.
    """

    provider_name = "OpenRouter"
    api_key_setting = "llm.openrouter.api_key"
    default_base_url = "https://openrouter.ai/api/v1"
    default_model = (
        "meta-llama/llama-3.2-3b-instruct:free"  # A free model as default
    )

    # Metadata for auto-discovery
    provider_key = "OPENROUTER"
    company_name = "OpenRouter"
    region = "US"
    country = "United States"
    data_location = "United States"
    is_cloud = True

    @classmethod
    def requires_auth_for_models(cls):
        """OpenRouter doesn't require authentication for listing models."""
        return False


# Keep the standalone functions for backward compatibility and registration
def create_openrouter_llm(model_name=None, temperature=0.7, **kwargs):
    """Factory function for OpenRouter LLMs using OpenAI-compatible endpoint.

    Args:
        model_name: Name of the model to use (e.g., "openai/gpt-4", "anthropic/claude-3-opus", etc.)
        temperature: Model temperature (0.0-1.0)
        **kwargs: Additional arguments including settings_snapshot

    Returns:
        A configured ChatOpenAI instance pointing to OpenRouter's API

    Raises:
        ValueError: If OpenRouter API key is not configured
    """
    return OpenRouterProvider.create_llm(model_name, temperature, **kwargs)


def is_openrouter_available(settings_snapshot=None):
    """Check if OpenRouter is available.

    Args:
        settings_snapshot: Optional settings snapshot to use

    Returns:
        True if OpenRouter API key is configured, False otherwise
    """
    return OpenRouterProvider.is_available(settings_snapshot)


def register_openrouter_provider():
    """Register the OpenRouter provider with the LLM registry."""
    register_llm("openrouter", create_openrouter_llm)
    logger.info("Registered OpenRouter LLM provider")
