"""Async PDF Reports service."""

from typing import Any, Dict, Optional
from sed_sh.services.async_base import AsyncBaseService


class AsyncReportsService(AsyncBaseService):
    """Async service for managing PDF report templates and generations."""

    async def create_template(
        self,
        name: str,
        html: str,
        expiry_hours: int = 24,
    ) -> Dict[str, Any]:
        """Create a new report template asynchronously.

        Args:
            name: Template name (for identification)
            html: HTML template content with Handlebars variables (max 1 MB)
            expiry_hours: Template expiry time in hours (max 24, default 24)

        Returns:
            Dict containing template information (see sync version for details)

        Raises:
            InvalidRequestError: If HTML exceeds 1 MB or parameters invalid
            AuthenticationError: If API key is invalid
            SedSHError: On other errors
        """
        payload = {
            "name": name,
            "html": html,
            "expiryHours": expiry_hours,
        }
        return await self._request("POST", "/reports/templates", json=payload)

    async def list_templates(self) -> Dict[str, Any]:
        """List all report templates asynchronously.

        Returns:
            Dict containing templates list (see sync version for details)

        Raises:
            AuthenticationError: If API key is invalid
            SedSHError: On other errors
        """
        return await self._request("GET", "/reports/templates")

    async def get_template(self, template_id: str) -> Dict[str, Any]:
        """Get a template with its HTML content asynchronously.

        Args:
            template_id: Template identifier

        Returns:
            Dict containing template details (see sync version for details)

        Raises:
            ResourceNotFoundError: If template doesn't exist
            AuthenticationError: If API key is invalid or not authorized
            SedSHError: On other errors
        """
        return await self._request("GET", f"/reports/templates/{template_id}")

    async def delete_template(self, template_id: str) -> Dict[str, Any]:
        """Delete a template asynchronously.

        Args:
            template_id: Template identifier

        Returns:
            Dict containing success confirmation

        Raises:
            ResourceNotFoundError: If template doesn't exist
            AuthenticationError: If API key is invalid or not authorized
            SedSHError: On other errors
        """
        return await self._request("DELETE", f"/reports/templates/{template_id}")

    async def generate_pdf(self, template_id: str, data: Dict[str, Any]) -> Dict[str, Any]:
        """Generate a PDF from a template and JSON data asynchronously.

        Args:
            template_id: Template identifier
            data: JSON data to merge into the Handlebars template

        Returns:
            Dict containing generation information (see sync version for details)

        Raises:
            ResourceNotFoundError: If template doesn't exist
            InvalidRequestError: If data is invalid or template compilation fails
            SedSHError: On other errors
        """
        return await self._request("POST", f"/reports/generate/{template_id}", json=data)

    async def list_generations(self) -> Dict[str, Any]:
        """List recent PDF generations asynchronously (last 100).

        Returns:
            Dict containing generations list (see sync version for details)

        Raises:
            AuthenticationError: If API key is invalid
            SedSHError: On other errors
        """
        return await self._request("GET", "/reports/generations")
