"""OpenAI Codex Responses provider."""

from __future__ import annotations

import asyncio
import hashlib
import json
from typing import Any, AsyncGenerator

from .base import LLMProvider, LLMResponse, ToolCallRequest

try:  # pragma: no cover - optional dependency
    import httpx
except ModuleNotFoundError:  # pragma: no cover
    httpx = None  # type: ignore[assignment]

try:  # pragma: no cover - optional dependency
    from oauth_cli_kit import get_token as get_codex_token
except ModuleNotFoundError:  # pragma: no cover
    get_codex_token = None  # type: ignore[assignment]

DEFAULT_CODEX_URL = "https://chatgpt.com/backend-api/codex/responses"
DEFAULT_ORIGINATOR = "beaver"


class OpenAICodexProvider(LLMProvider):
    """使用 Codex OAuth 调用 Responses API。"""

    def __init__(
        self,
        default_model: str = "openai-codex/gpt-5.1-codex",
        request_timeout_seconds: float | None = None,
    ) -> None:
        super().__init__(api_key=None, api_base=None, request_timeout_seconds=request_timeout_seconds)
        self.default_model = default_model

    async def chat(
        self,
        messages: list[dict[str, Any]],
        tools: list[dict[str, Any]] | None = None,
        model: str | None = None,
        max_tokens: int | None = None,
        temperature: float = 0.7,
        thinking_enabled: bool | None = None,
    ) -> LLMResponse:
        if httpx is None or get_codex_token is None:
            return LLMResponse(content="Error: codex dependencies are not installed", finish_reason="error", provider_name="openai_codex")

        resolved_model = model or self.default_model
        system_prompt, input_items = _convert_messages(messages)
        token = await asyncio.to_thread(get_codex_token)
        headers = _build_headers(token.account_id, token.access)
        body: dict[str, Any] = {
            "model": _strip_model_prefix(resolved_model),
            "store": False,
            "stream": True,
            "instructions": system_prompt,
            "input": input_items,
            "text": {"verbosity": "medium"},
            "include": ["reasoning.encrypted_content"],
            "prompt_cache_key": _prompt_cache_key(messages),
            "tool_choice": "auto",
            "parallel_tool_calls": True,
        }
        if tools:
            body["tools"] = _convert_tools(tools)

        try:
            content, tool_calls, finish_reason = await _request_codex(
                DEFAULT_CODEX_URL,
                headers,
                body,
                verify=True,
                timeout_seconds=self.request_timeout_seconds or 600.0,
            )
        except Exception as exc:
            return LLMResponse(content=f"Error calling Codex: {exc}", finish_reason="error", provider_name="openai_codex")

        return LLMResponse(
            content=content,
            tool_calls=tool_calls,
            finish_reason=finish_reason,
            provider_name="openai_codex",
            model=resolved_model,
        )

    def get_default_model(self) -> str:
        return self.default_model


def _strip_model_prefix(model: str) -> str:
    if model.startswith("openai-codex/") or model.startswith("openai_codex/"):
        return model.split("/", 1)[1]
    return model


def _build_headers(account_id: str, token: str) -> dict[str, str]:
    return {
        "Authorization": f"Bearer {token}",
        "chatgpt-account-id": account_id,
        "OpenAI-Beta": "responses=experimental",
        "originator": DEFAULT_ORIGINATOR,
        "User-Agent": "beaver (python)",
        "accept": "text/event-stream",
        "content-type": "application/json",
    }


async def _request_codex(
    url: str,
    headers: dict[str, str],
    body: dict[str, Any],
    verify: bool,
    timeout_seconds: float,
) -> tuple[str, list[ToolCallRequest], str]:
    async with httpx.AsyncClient(timeout=timeout_seconds, verify=verify) as client:
        async with client.stream("POST", url, headers=headers, json=body) as response:
            if response.status_code != 200:
                text = await response.aread()
                raise RuntimeError(_friendly_error(response.status_code, text.decode("utf-8", "ignore")))
            return await _consume_sse(response)


def _convert_tools(tools: list[dict[str, Any]]) -> list[dict[str, Any]]:
    converted: list[dict[str, Any]] = []
    for tool in tools:
        fn = (tool.get("function") or {}) if tool.get("type") == "function" else tool
        name = fn.get("name")
        if not name:
            continue
        params = fn.get("parameters") or {}
        converted.append(
            {
                "type": "function",
                "name": name,
                "description": fn.get("description") or "",
                "parameters": params if isinstance(params, dict) else {},
            }
        )
    return converted


def _convert_messages(messages: list[dict[str, Any]]) -> tuple[str, list[dict[str, Any]]]:
    system_prompt = ""
    input_items: list[dict[str, Any]] = []
    for index, message in enumerate(messages):
        role = message.get("role")
        content = message.get("content")
        if role == "system":
            system_prompt = content if isinstance(content, str) else ""
            continue
        if role == "user":
            input_items.append(_convert_user_message(content))
            continue
        if role == "assistant":
            if isinstance(content, str) and content:
                input_items.append(
                    {
                        "type": "message",
                        "role": "assistant",
                        "content": [{"type": "output_text", "text": content}],
                        "status": "completed",
                        "id": f"msg_{index}",
                    }
                )
            for tool_call in message.get("tool_calls", []) or []:
                fn = tool_call.get("function") or {}
                call_id, item_id = _split_tool_call_id(tool_call.get("id"))
                input_items.append(
                    {
                        "type": "function_call",
                        "id": item_id or f"fc_{index}",
                        "call_id": call_id or f"call_{index}",
                        "name": fn.get("name"),
                        "arguments": fn.get("arguments") or "{}",
                    }
                )
            continue
        if role == "tool":
            call_id, _ = _split_tool_call_id(message.get("tool_call_id"))
            output_text = content if isinstance(content, str) else json.dumps(content, ensure_ascii=False)
            input_items.append(
                {
                    "type": "function_call_output",
                    "call_id": call_id,
                    "output": output_text,
                }
            )
    return system_prompt, input_items


def _convert_user_message(content: Any) -> dict[str, Any]:
    if isinstance(content, str):
        return {"role": "user", "content": [{"type": "input_text", "text": content}]}
    if isinstance(content, list):
        converted: list[dict[str, Any]] = []
        for item in content:
            if not isinstance(item, dict):
                continue
            if item.get("type") == "text":
                converted.append({"type": "input_text", "text": item.get("text", "")})
            elif item.get("type") == "image_url":
                url = (item.get("image_url") or {}).get("url")
                if url:
                    converted.append({"type": "input_image", "image_url": url, "detail": "auto"})
        if converted:
            return {"role": "user", "content": converted}
    return {"role": "user", "content": [{"type": "input_text", "text": ""}]}


def _split_tool_call_id(tool_call_id: Any) -> tuple[str, str | None]:
    if isinstance(tool_call_id, str) and tool_call_id:
        if "|" in tool_call_id:
            call_id, item_id = tool_call_id.split("|", 1)
            return call_id, item_id or None
        return tool_call_id, None
    return "call_0", None


def _prompt_cache_key(messages: list[dict[str, Any]]) -> str:
    raw = json.dumps(messages, ensure_ascii=True, sort_keys=True)
    return hashlib.sha256(raw.encode("utf-8")).hexdigest()


async def _iter_sse(response: Any) -> AsyncGenerator[dict[str, Any], None]:
    buffer: list[str] = []
    async for line in response.aiter_lines():
        if line == "":
            if buffer:
                data_lines = [item[5:].strip() for item in buffer if item.startswith("data:")]
                buffer = []
                if not data_lines:
                    continue
                data = "\n".join(data_lines).strip()
                if not data or data == "[DONE]":
                    continue
                try:
                    yield json.loads(data)
                except Exception:
                    continue
            continue
        buffer.append(line)


async def _consume_sse(response: Any) -> tuple[str, list[ToolCallRequest], str]:
    content_parts: list[str] = []
    tool_calls: list[ToolCallRequest] = []
    finish_reason = "stop"
    async for event in _iter_sse(response):
        event_type = event.get("type")
        if event_type == "response.output_text.delta":
            delta = event.get("delta") or ""
            content_parts.append(delta)
        elif event_type == "response.output_item.added":
            item = event.get("item") or {}
            if item.get("type") == "function_call":
                raw_arguments = item.get("arguments") or "{}"
                try:
                    arguments = json.loads(raw_arguments) if isinstance(raw_arguments, str) else raw_arguments
                except json.JSONDecodeError:
                    arguments = {}
                tool_calls.append(
                    ToolCallRequest(
                        id=f"{item.get('call_id', 'call')}|{item.get('id', '')}",
                        name=item.get("name", ""),
                        arguments=arguments,
                    )
                )
        elif event_type == "response.completed":
            finish_reason = event.get("response", {}).get("status", "completed")
    return "".join(content_parts) or None, tool_calls, finish_reason


def _friendly_error(status_code: int, body: str) -> str:
    return f"Codex API error ({status_code}): {body[:400]}"
