from __future__ import annotations

from typing import TYPE_CHECKING

from ..objects.agent import Agent
from ..objects.list_response import ListResponse

if TYPE_CHECKING:
    from ..client import WhatsrbCloud
    from ..connection import Connection


class Agents:
    def __init__(self, *, client: WhatsrbCloud, connection: Connection):
        self._client = client
        self._connection = connection

    def list(self) -> ListResponse[Agent]:
        response = self._connection.get("/agents")
        data = [Agent(a, client=self._client) for a in (response.get("data") or [])]
        return ListResponse(data=data, meta=response.get("meta", {}))

    def create(self, **params) -> Agent:
        response = self._connection.post("/agents", {"agent": params})
        return Agent(response["data"], client=self._client)

    def retrieve(self, id: str) -> Agent:
        response = self._connection.get(f"/agents/{id}")
        return Agent(response["data"], client=self._client)

    def update(self, id: str, **params) -> Agent:
        response = self._connection.patch(f"/agents/{id}", {"agent": params})
        return Agent(response["data"], client=self._client)

    def delete(self, id: str) -> bool:
        self._connection.delete(f"/agents/{id}")
        return True

    def runs(self, agent_id: str):
        from .agent_runs import AgentRuns
        return AgentRuns(client=self._client, connection=self._connection, agent_id=agent_id)
