from typing import Any

from synth_ai.core.rust_core.http import RustCoreHttpClient
from synth_ai.core.rust_core.urls import ensure_api_base


async def backend_health(base_url: str, api_key: str) -> dict[str, Any]:
    async with RustCoreHttpClient(base_url, api_key, timeout=15.0) as http:
        js = await http.get(f"{ensure_api_base(base_url)}/health")
    return {"ok": True, "raw": js}


async def container_health(container_url: str) -> dict[str, Any]:
    # Delegate to central task module for consistency
    from synth_ai.sdk.container._impl.health import container_health as _th

    return await _th(container_url)


async def pricing_preflight(
    base_url: str,
    api_key: str,
    *,
    job_type: str,
    gpu_type: str,
    estimated_seconds: float,
    container_count: int,
) -> dict[str, Any]:
    body = {
        "job_type": job_type,
        "gpu_type": gpu_type,
        "estimated_seconds": float(estimated_seconds or 0.0),
        "container_count": int(container_count or 1),
    }
    async with RustCoreHttpClient(base_url, api_key, timeout=30.0) as http:
        js = await http.post_json(
            f"{ensure_api_base(base_url)}/v1/pricing/preflight",
            json=body,
        )
    return js if isinstance(js, dict) else {"raw": js}


async def balance_autumn_normalized(base_url: str, api_key: str) -> dict[str, Any]:
    async with RustCoreHttpClient(base_url, api_key, timeout=30.0) as http:
        js = await http.get(f"{ensure_api_base(base_url)}/v1/balance/autumn-normalized")
    return js if isinstance(js, dict) else {"raw": js}
