from __future__ import annotations

"""Async base resource class for SDK HTTP operations."""

from typing import Any

import httpx

from nexure.exceptions import _handle_response


class AsyncBaseResource:
    """Base class for all async SDK resource namespaces."""

    def __init__(self, client: httpx.AsyncClient):
        self._client = client

    async def _get(self, path: str, params: dict[str, Any] | None = None) -> dict[str, Any]:
        """Send an async GET request and return parsed JSON."""
        resp = await self._client.get(path, params=params)
        body = resp.json()
        _handle_response(resp.status_code, body, dict(resp.headers))
        return body

    async def _post(self, path: str, json: dict[str, Any] | None = None) -> dict[str, Any]:
        """Send an async POST request and return parsed JSON."""
        resp = await self._client.post(path, json=json)
        body = resp.json()
        _handle_response(resp.status_code, body, dict(resp.headers))
        return body

    async def _put(self, path: str, json: dict[str, Any] | None = None) -> dict[str, Any]:
        """Send an async PUT request and return parsed JSON."""
        resp = await self._client.put(path, json=json)
        body = resp.json()
        _handle_response(resp.status_code, body, dict(resp.headers))
        return body

    async def _patch(self, path: str, json: dict[str, Any] | None = None) -> dict[str, Any]:
        """Send an async PATCH request and return parsed JSON."""
        resp = await self._client.patch(path, json=json)
        body = resp.json()
        _handle_response(resp.status_code, body, dict(resp.headers))
        return body

    async def _delete(self, path: str) -> None:
        """Send an async DELETE request (expects 204 No Content)."""
        resp = await self._client.delete(path)
        if resp.status_code >= 400:
            body = resp.json() if resp.content else {}
            _handle_response(resp.status_code, body, dict(resp.headers))
