from __future__ import annotations

from typing import Any
from uuid import UUID

from .base import SubclientBase
from ..schemas import CreateTaskRequest, TaskResponse


class TasksClient(SubclientBase):
    async def create_task(self, request: CreateTaskRequest) -> TaskResponse:
        response = await self._client.post(
            self._client.path("/tasks"), json=self._client.dump(request)
        )
        return self._client.parse(response, TaskResponse)

    async def list_tasks(
        self,
        created_by: UUID | None = None,
        assignee: UUID | None = None,
        offset: int = 0,
        limit: int = 1000,
        item_id: UUID | None = None,
    ) -> list[TaskResponse]:
        params: dict[str, Any] = {
            "offset": offset,
            "limit": limit,
        }
        if created_by is not None:
            params["created_by"] = str(created_by)
        if assignee is not None:
            params["assignee"] = str(assignee)
        if item_id is not None:
            params["itemId"] = str(item_id)

        response = await self._client.get(self._client.path("/tasks"), params=params)
        return self._client.parse_list(response, TaskResponse)

    async def get_task(self, task_id: UUID) -> TaskResponse:
        response = await self._client.get(self._client.path(f"/tasks/{task_id}"))
        return self._client.parse(response, TaskResponse)
