"""Task resource for the Daly Energy SDK.

Tasks track the status of asynchronous operations (energy model runs,
shading computations, etc.).
"""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from dalysdk.client import DalyClient


class TaskResource:
    """Interact with the ``/tasks`` endpoints.

    Usage::

        # List tasks with filtering
        result = client.tasks.list()
        result = client.tasks.list(status="completed", page=1, page_size=20)

        # Get a specific task
        task = client.tasks.get(42)
    """

    def __init__(self, client: DalyClient):
        self._client = client

    def list(
        self,
        *,
        status: str | None = None,
        task_type: str | None = None,
        page: int = 1,
        page_size: int = 50,
    ) -> dict:
        """List tasks with optional filtering and pagination.

        Args:
            status: Filter by task status (e.g. ``"pending"``, ``"running"``,
                    ``"completed"``, ``"failed"``).
            task_type: Filter by task type (e.g. ``"energy_model"``,
                       ``"shading"``).
            page: Page number (1-based). Default 1.
            page_size: Number of tasks per page (1-100). Default 50.

        Returns:
            Dict with ``tasks`` (list), ``total`` (count), ``page``, and
            ``pageSize``.
        """
        params: dict = {"page": page, "page_size": page_size}
        if status is not None:
            params["status"] = status
        if task_type is not None:
            params["task_type"] = task_type
        return self._client._request("GET", "/tasks", params=params)

    def get(self, task_id: int) -> dict:
        """Get a task by its workspace-scoped ID.

        Args:
            task_id: Workspace-scoped task index.

        Returns:
            Task dict with status, type, timestamps, and result data.

        Raises:
            NotFoundError: If the task does not exist.
        """
        return self._client._request("GET", f"/tasks/{task_id}")
