"""Anthropic provider adapter for agentbudget."""

from typing import Any
from agentbudget.providers.base import BaseProvider, UsageData


class AnthropicProvider(BaseProvider):
    """Provider adapter for Anthropic API responses.

    Handles Message response objects from the anthropic SDK.
    Extracts input_tokens and output_tokens from response.usage.
    """

    def extract_usage(self, response: Any) -> UsageData:
        """Extract token usage from an Anthropic Message response.

        Args:
            response: An anthropic.types.Message object.

        Returns:
            UsageData with model, input_tokens, output_tokens, provider="anthropic".

        Raises:
            ValueError: If the response has no usage attribute or is malformed.
        """
        if not hasattr(response, "usage") or response.usage is None:
            raise ValueError("Anthropic response has no usage data.")

        model = getattr(response, "model", "unknown")
        usage = response.usage
        input_tokens = getattr(usage, "input_tokens", 0) or 0
        output_tokens = getattr(usage, "output_tokens", 0) or 0

        return UsageData(
            model=model,
            input_tokens=input_tokens,
            output_tokens=output_tokens,
            provider="anthropic",
        )
