from __future__ import annotations

from .base import SubclientBase
from ..schemas import QueryRequest, ReportGenerationResponse


class ReportsClient(SubclientBase):

    async def start_report_generation(
        self,
        request: QueryRequest,
        include_alias: bool = False,
        output_format: str = "excel",
    ) -> ReportGenerationResponse:
        response = await self._client.post(
            self._client.path("/reports/items"),
            json=self._client.dump(request),
            params={"includeAlias": include_alias, "outputFormat": output_format},
        )
        return self._client.parse(response, ReportGenerationResponse)

    async def download_report(
        self,
        job_id: str,
        timeout_seconds: float | None = 300.0,
        poll_interval_seconds: float = 1.0,
        wait_for_ready: bool = True,
    ) -> bytes:
        if not job_id:
            raise ValueError("job_id must be a non-empty string")

        if timeout_seconds is not None and timeout_seconds < 0:
            raise ValueError("timeout_seconds must be non-negative or None")
        if poll_interval_seconds <= 0:
            raise ValueError("poll_interval_seconds must be greater than 0")

        if wait_for_ready:
            await self._client.bgtasks.wait_for_task(
                job_id=job_id,
                timeout_seconds=timeout_seconds,
                poll_interval_seconds=poll_interval_seconds,
            )

        return await self._client.get_bytes(self._client.path(f"/reports/{job_id}"))
