from __future__ import annotations

import asyncio
from typing import Any

import requests

from .config import TIANWANG_CALLBACK_URL


async def post_tianwang_callback(event: str, data: dict[str, Any], extra: dict[str, Any] | None = None, timeout_s: int = 5) -> tuple[bool, int, str]:
    payload = {
        'event': event,
        'data': data,
        'extra': extra or {},
    }

    def _post() -> tuple[bool, int, str]:
        try:
            resp = requests.post(TIANWANG_CALLBACK_URL, json=payload, timeout=timeout_s)
            ok = 200 <= resp.status_code < 300
            return ok, resp.status_code, (resp.text or '')[:300]
        except Exception as e:
            return False, 0, str(e)

    return await asyncio.to_thread(_post)
