"""Custom Anthropic-compatible (Messages API) endpoint provider.

Connects to a self-hosted LLM service that speaks the Anthropic Messages API
(``/v1/messages``) rather than the OpenAI chat-completions format. Mirrors
``CustomOpenAIEndpointProvider`` but builds a ``ChatAnthropic`` client (via the
parent ``AnthropicProvider``) pointed at an operator-configured ``base_url``.
"""

from loguru import logger

from ....config.thread_settings import get_setting_from_snapshot
from ....security.log_sanitizer import redact_secrets
from .anthropic import AnthropicProvider


class CustomAnthropicEndpointProvider(AnthropicProvider):
    """Custom Anthropic-format endpoint provider.

    Lets users connect to any service implementing the Anthropic Messages API
    by specifying a base URL in settings. The URL is SSRF-validated and passed
    to ``ChatAnthropic`` by the inherited ``create_llm`` (which activates its
    URL/keyless branch because ``url_setting`` is set here).
    """

    provider_name = "Anthropic-Compatible Endpoint"
    api_key_setting = "llm.anthropic_endpoint.api_key"
    # Many self-hosted Anthropic-format gateways don't require auth; fall back
    # to the shared placeholder instead of raising at construction time.
    api_key_optional = True
    url_setting = "llm.anthropic_endpoint.url"  # type: ignore[assignment]

    # Metadata for auto-discovery (registered as the "anthropic_endpoint"
    # provider string). The egress policy classifies it generically via
    # ``llm.anthropic_endpoint.url`` — keep the setting name in sync.
    provider_key = "ANTHROPIC_ENDPOINT"
    company_name = "Anthropic-Compatible"
    is_cloud = None  # Unknown — could be local or cloud

    @classmethod
    def requires_auth_for_models(cls):
        """Self-hosted Anthropic-format endpoints may allow keyless listing.

        Return False so model listing is attempted without an API key. If the
        endpoint requires auth, the Anthropic client raises and we degrade to
        an empty list.
        """
        return False

    @classmethod
    def is_available(cls, settings_snapshot=None):
        """Available when either an API key or a custom URL is configured.

        Unlike the cloud Anthropic provider, this custom endpoint supports
        keyless local servers — so a configured URL alone makes it usable.
        """
        api_key = None
        try:
            api_key = get_setting_from_snapshot(
                cls.api_key_setting,
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if api_key and str(api_key).strip():
                return True
        except Exception as e:
            # Redact in case a settings-layer error message embeds the key.
            safe_msg = redact_secrets(str(e), api_key)
            logger.debug(f"Error checking provider availability: {safe_msg}")

        try:
            custom_url = get_setting_from_snapshot(
                cls.url_setting,
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if custom_url and str(custom_url).strip():
                return True
        except Exception:
            logger.debug(
                f"Error reading URL setting '{cls.url_setting}'",
                exc_info=True,
            )

        return False

    # list_models_for_api is inherited from AnthropicProvider, which lists
    # models via the anthropic SDK and honors cls.url_setting (SSRF-guarding
    # the configured base_url; returning [] when no URL is set).
