"""Provider-level capability defaults with deterministic family pattern matching."""

from __future__ import annotations

from typing import Final

from ..providers.features import Cap

FULL_SUITE = Cap.TOOLS | Cap.JSON | Cap.STRUCTURED_OUTPUT
TOOLS_AND_JSON = Cap.TOOLS | Cap.JSON
TOOLS_JSON_REASONING = Cap.TOOLS | Cap.JSON | Cap.REASONING
TOOL_ONLY = Cap.TOOLS

# Provider family patterns for capability inheritance.
# Keys are provider names, values are dicts mapping model prefix → capabilities.
PROVIDER_FAMILY_DEFAULTS: Final[dict[str, dict[str, Cap]]] = {
    "openai": {
        "gpt-4": FULL_SUITE,
        "gpt-5": FULL_SUITE | Cap.REASONING,
        "gpt-3.5-turbo": TOOLS_AND_JSON,
        "o1": TOOL_ONLY,
        "o3": TOOL_ONLY,
        "o4-mini": TOOL_ONLY,
    },
    "anthropic": {
        "claude-3-5": TOOLS_JSON_REASONING,
        "claude-3-7": TOOLS_JSON_REASONING,
        "claude-3.5": TOOLS_JSON_REASONING,
        "claude-3.7": TOOLS_JSON_REASONING,
        "claude-4": TOOLS_JSON_REASONING,
        "claude-opus-4": TOOLS_JSON_REASONING,
        "claude-sonnet-4": TOOLS_JSON_REASONING,
        "claude-haiku-4": TOOLS_JSON_REASONING,
        "claude-3": TOOLS_AND_JSON,
    },
    "google": {
        "gemini-1.5": FULL_SUITE,
        "gemini-2.5": FULL_SUITE,
    },
    "azure_openai": {
        "gpt-4": FULL_SUITE,
        "gpt-3.5-turbo": TOOLS_AND_JSON,
    },
}

_SORTED_FAMILY_DEFAULTS: Final[dict[str, tuple[tuple[str, Cap], ...]]] = {
    provider: tuple(sorted(patterns.items(), key=lambda item: len(item[0]), reverse=True))
    for provider, patterns in PROVIDER_FAMILY_DEFAULTS.items()
}

# Provider-wide fallback defaults when no family pattern matches.
PROVIDER_DEFAULTS: Final[dict[str, Cap]] = {
    "openai": TOOLS_AND_JSON,
    "anthropic": TOOLS_AND_JSON,
    "google": TOOLS_AND_JSON,
    "azure_openai": TOOLS_AND_JSON,
}


def model_matches_pattern(model_id: str, pattern: str) -> bool:
    """Check if a model ID matches a family pattern using prefix matching.

    Args:
        model_id: The model identifier to check (e.g., "gpt-4o-mini")
        pattern: The family pattern to match against (e.g., "gpt-4")

    Returns:
        True if model_id starts with pattern (case-insensitive)

    Examples:
        >>> model_matches_pattern("gpt-4o-mini", "gpt-4")
        True
        >>> model_matches_pattern("gpt-4-turbo", "gpt-4")
        True
        >>> model_matches_pattern("gpt-3.5-turbo", "gpt-4")
        False
        >>> model_matches_pattern("claude-3-opus-20240229", "claude-3")
        True
    """
    return model_id.lower().startswith(pattern.lower())


def resolve_family_capabilities(model_id: str, provider: str) -> Cap:
    """Resolve capabilities from provider family patterns.

    Args:
        model_id: The model identifier
        provider: The provider name

    Returns:
        Capabilities bitset (Cap.NONE if no family pattern matches)
    """
    for pattern, caps in _SORTED_FAMILY_DEFAULTS.get(provider, ()):
        if model_matches_pattern(model_id, pattern):
            return caps

    return Cap.NONE


__all__ = [
    "PROVIDER_DEFAULTS",
    "PROVIDER_FAMILY_DEFAULTS",
    "model_matches_pattern",
    "resolve_family_capabilities",
]
