"""OAuth utility functions for Anthropic subscription auth."""

from typing import Any


def is_oauth_token(token: str | None) -> bool:
    """Check if token is an OAuth token (vs regular API key).

    OAuth tokens from Claude Max/Pro contain 'sk-ant-oat' prefix.
    Regular API keys use 'sk-ant-api03' or similar.
    """
    if not token:
        return False
    return "sk-ant-oat" in token


def get_auth_headers(token: str, is_oauth: bool = False) -> dict[str, str]:
    """Get authentication headers for Anthropic API.

    OAuth tokens require Authorization: Bearer header.
    Regular API keys use x-api-key header.
    """
    headers: dict[str, str] = {
        "anthropic-version": "2023-06-01",
        "content-type": "application/json",
    }

    if is_oauth:
        headers["Authorization"] = f"Bearer {token}"
        # Required headers to mimic Claude Code client
        headers["anthropic-beta"] = "claude-code-20250219,oauth-2025-04-20,context-management-2025-06-27"
        headers["anthropic-dangerous-direct-browser-access"] = "true"
        headers["user-agent"] = "claude-cli/2.1.2 (external, cli)"
        headers["x-app"] = "cli"
    else:
        headers["x-api-key"] = token

    return headers


def get_claude_code_system_prefix() -> str:
    """Get the required system prompt prefix for OAuth tokens.

    Anthropic requires this identity declaration for OAuth auth.
    """
    return "You are a Claude agent, built on Anthropic's Claude Agent SDK."
