"""Anthropic API provider for ShelfAI."""

from shelfai.providers import LLMProvider


class AnthropicProvider(LLMProvider):
    """
    LLM provider using the Anthropic Messages API.

    Requires: pip install shelfai[anthropic]
    """

    def __init__(self, model: str = "claude-sonnet-4-20250514", api_key: str = None):
        """
        Initialize the Anthropic provider.

        Args:
            model: Model name (default: claude-sonnet-4-20250514)
            api_key: API key. If None, uses ANTHROPIC_API_KEY env var.
        """
        try:
            import anthropic
        except ImportError:
            raise ImportError(
                "Anthropic SDK not installed. Run: pip install shelfai[anthropic]"
            )

        self.client = anthropic.Anthropic(api_key=api_key)
        self.model = model

        # Fix #33: Declare retryable errors for the retry logic
        self.retryable_errors = (
            anthropic.RateLimitError,
            anthropic.InternalServerError,
            anthropic.APIConnectionError,
        )

    def complete(self, system: str, user: str, max_tokens: int = 4096) -> str:
        """Send a completion request to the Anthropic API."""
        response = self.client.messages.create(
            model=self.model,
            max_tokens=max_tokens,
            system=system,
            messages=[{"role": "user", "content": user}],
        )
        return response.content[0].text
