"""
ShelfAI LLM providers.

Provides a unified interface for calling different LLM APIs.
Providers are optional dependencies — only install what you need.
"""

from abc import ABC, abstractmethod

from shelfai.core.config import ShelfConfig


class LLMProvider(ABC):
    """
    Base interface for LLM providers used by ShelfAI agents.

    All providers implement a single `complete()` method that takes
    a system prompt and user message and returns a response string.
    """

    @abstractmethod
    def complete(self, system: str, user: str, max_tokens: int = 4096) -> str:
        """
        Send a completion request to the LLM.

        Args:
            system: System prompt
            user: User message
            max_tokens: Maximum response tokens

        Returns:
            The model's response text
        """
        pass


def get_provider(config: ShelfConfig) -> LLMProvider:
    """
    Create an LLM provider from shelf configuration.

    Args:
        config: Shelf configuration with provider settings

    Returns:
        Configured LLMProvider instance

    Raises:
        ValueError: If the provider type is not recognized
        ImportError: If the required provider package isn't installed
    """
    import os

    provider_name = config.session.provider.lower()
    model = config.session.model
    api_key = os.environ.get(config.session.api_key_env)

    if provider_name == "anthropic":
        from shelfai.providers.anthropic import AnthropicProvider

        return AnthropicProvider(model=model, api_key=api_key)

    elif provider_name in ("openai", "gpt"):
        from shelfai.providers.openai import OpenAIProvider

        return OpenAIProvider(model=model, api_key=api_key)

    else:
        raise ValueError(
            f"Unknown provider: '{provider_name}'. "
            f"Supported providers: anthropic, openai"
        )
