# retoor <retoor@molodetz.nl>

from dataclasses import dataclass
from typing import Any, Optional

CLIENT_THINKING_KEYS = (
    "think",
    "thinking",
    "reasoning",
    "reasoning_effort",
    "enable_thinking",
)

FALSE_VALUES = {False, 0, "0", "false", "no", "off", "none", "disabled"}
TRUE_VALUES = {True, 1, "1", "true", "yes", "on", "enabled"}
EFFORT_VALUES = {"low", "medium", "high", "max", "xhigh"}


def thinking_dialect(url: str) -> str:
    text = (url or "").lower()
    if "deepseek.com" in text:
        return "deepseek"
    if "openrouter.ai" in text:
        return "openrouter"
    if "11434" in text or "ollama" in text:
        return "ollama"
    return "deepseek"


def _resolve_dialect(url: str, dialect: str) -> str:
    effective = dialect.strip().lower()
    if effective in ("", "auto"):
        return thinking_dialect(url)
    return effective


@dataclass(frozen=True)
class UpstreamCapabilities:
    dialect: str
    supports_stream_options: bool
    supports_stream_usage: bool
    supports_thinking_field: bool


def upstream_capabilities(
    url: str, dialect: str = ""
) -> UpstreamCapabilities:
    effective = _resolve_dialect(url, dialect)
    if effective == "ollama":
        return UpstreamCapabilities(
            dialect=effective,
            supports_stream_options=False,
            supports_stream_usage=False,
            supports_thinking_field=True,
        )
    return UpstreamCapabilities(
        dialect=effective,
        supports_stream_options=True,
        supports_stream_usage=True,
        supports_thinking_field=True,
    )


def _as_text(value: Any) -> str:
    if isinstance(value, str):
        return value.strip().lower()
    return ""


def _is_false(value: Any) -> bool:
    if isinstance(value, str):
        return value.strip().lower() in FALSE_VALUES
    return value in FALSE_VALUES


def _is_true(value: Any) -> bool:
    if isinstance(value, str):
        text = value.strip().lower()
        return text in TRUE_VALUES or text in EFFORT_VALUES
    return value in TRUE_VALUES


def client_thinking_enabled(body: Any) -> Optional[bool]:
    if not isinstance(body, dict):
        return None
    thinking = body.get("thinking")
    if isinstance(thinking, dict) and "type" in thinking:
        kind = _as_text(thinking.get("type"))
        if kind in ("disabled", "none", "off"):
            return False
        if kind in ("enabled", "on"):
            return True
    if "think" in body:
        value = body.get("think")
        if _is_false(value):
            return False
        if _is_true(value):
            return True
        return bool(value)
    reasoning = body.get("reasoning")
    if isinstance(reasoning, dict):
        if "enabled" in reasoning:
            return _flag_enabled(reasoning.get("enabled"))
        effort = _as_text(reasoning.get("effort"))
        if effort == "none":
            return False
        if effort in EFFORT_VALUES:
            return True
    if "reasoning_effort" in body:
        effort = _as_text(body.get("reasoning_effort"))
        if effort == "none":
            return False
        if effort in EFFORT_VALUES:
            return True
    if "enable_thinking" in body:
        return _flag_enabled(body.get("enable_thinking"))
    kwargs = body.get("chat_template_kwargs")
    if isinstance(kwargs, dict) and "enable_thinking" in kwargs:
        return _flag_enabled(kwargs.get("enable_thinking"))
    return None


def _flag_enabled(value: Any) -> bool:
    if _is_false(value):
        return False
    if _is_true(value):
        return True
    return bool(value)


def _clear_thinking_fields(payload: dict) -> None:
    for key in CLIENT_THINKING_KEYS:
        payload.pop(key, None)
    kwargs = payload.get("chat_template_kwargs")
    if isinstance(kwargs, dict):
        kwargs.pop("enable_thinking", None)
        if not kwargs:
            payload.pop("chat_template_kwargs", None)


def write_thinking(payload: dict, dialect: str, enabled: bool) -> None:
    _clear_thinking_fields(payload)
    if dialect == "openrouter":
        payload["reasoning"] = {"effort": "high" if enabled else "none"}
        return
    if dialect == "ollama":
        payload["think"] = bool(enabled)
        return
    payload["thinking"] = {"type": "enabled" if enabled else "disabled"}


def apply_thinking(
    payload: dict,
    upstream_url: str,
    default_enabled: bool = False,
    dialect: str = "",
) -> dict:
    effective = _resolve_dialect(upstream_url, dialect)
    specified = client_thinking_enabled(payload)
    enabled = default_enabled if specified is None else specified
    write_thinking(payload, effective, enabled)
    return payload
