"""Anthropic Claude LLM provider implementation."""

import httpx
from typing import Dict, Any
from .base import LLMProvider, LLMRequest, LLMResponse, ProviderNotConfiguredError
import logging

logger = logging.getLogger(__name__)


class ClaudeProvider(LLMProvider):
    """Anthropic Claude LLM provider."""

    def __init__(self, config: Dict[str, Any]):
        super().__init__(config)
        self.base_url = config.get("base_url", "https://api.anthropic.com")
        self.timeout = config.get("timeout", 30)

    def _make_headers(self) -> Dict[str, str]:
        return {
            "x-api-key": self.api_key,
            "Content-Type": "application/json",
            "anthropic-version": "2023-06-01",
        }

    async def generate(self, request: LLMRequest) -> LLMResponse:
        """Generate response using Claude API."""
        self.ensure_enabled()

        try:
            system_prompt = request.system_prompt or ""
            messages = [{"role": "user", "content": request.prompt}]

            payload = {
                "model": self.model,
                "messages": messages,
                "max_tokens": request.max_tokens,
                "temperature": request.temperature,
            }

            if system_prompt:
                payload["system"] = system_prompt

            if request.stop_sequences:
                payload["stop_sequences"] = request.stop_sequences

            async with httpx.AsyncClient(
                base_url=self.base_url,
                headers=self._make_headers(),
                timeout=self.timeout,
            ) as client:
                response = await client.post("/v1/messages", json=payload)
            response.raise_for_status()

            data = response.json()
            content = data["content"][0]["text"] if data["content"] else ""
            usage = data.get("usage", {})

            return LLMResponse(
                content=content,
                provider=self.provider_name,
                model=self.model,
                usage={
                    "prompt_tokens": usage.get("input_tokens", 0),
                    "completion_tokens": usage.get("output_tokens", 0),
                    "total_tokens": usage.get("input_tokens", 0) + usage.get("output_tokens", 0)
                },
                metadata={
                    "stop_reason": data.get("stop_reason"),
                    "request_id": response.headers.get("request-id")
                }
            )

        except httpx.TimeoutException:
            error_msg = "Claude request timed out"
            logger.error(error_msg)
            return LLMResponse(content="", provider=self.provider_name, model=self.model, error=error_msg)
        except httpx.HTTPStatusError as e:
            error_msg = f"HTTP {e.response.status_code}: {e.response.text}"
            logger.error(f"Claude API error: {error_msg}")
            return LLMResponse(content="", provider=self.provider_name, model=self.model, error=error_msg)
        except Exception as e:
            error_msg = f"Claude provider error: {type(e).__name__}: {e}" if str(e) else f"Claude provider error: {type(e).__name__}"
            logger.error(error_msg)
            return LLMResponse(content="", provider=self.provider_name, model=self.model, error=error_msg)
