"""
Pulse columns resource — CRUD for custom headline columns.
"""
from typing import Any, Dict, Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from kronical.client import KronicalClient, AsyncKronicalClient


class PulseResource:
    """Synchronous pulse column methods."""

    def __init__(self, client: "KronicalClient"):
        self._client = client

    def list(self) -> Dict[str, Any]:
        """
        GET /v1/pulse — list all pulse columns.

        Returns:
            {"columns": [...], "count": int}
        """
        return self._client.request("GET", "/v1/pulse")

    def create(
        self,
        name: str,
        query: str,
        *,
        filters: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        """
        POST /v1/pulse — create a new pulse column.

        Args:
            name: Display name for the column.
            query: Natural language query that defines the column.
            filters: Optional filters (novelty, escalation, etc.).

        Returns:
            Created pulse column dict.
        """
        body: Dict[str, Any] = {"name": name, "query": query}
        if filters is not None:
            body["filters"] = filters
        return self._client.request("POST", "/v1/pulse", json=body)

    def update(
        self,
        column_id: str,
        *,
        name: Optional[str] = None,
        query: Optional[str] = None,
        filters: Optional[Dict[str, Any]] = None,
        enabled: Optional[bool] = None,
    ) -> Dict[str, Any]:
        """
        PUT /v1/pulse/{column_id} — update a pulse column.

        Args:
            column_id: ID of the column to update.
            name: New display name.
            query: New query string.
            filters: New filters.
            enabled: Enable or disable the column.

        Returns:
            Updated pulse column dict.
        """
        body: Dict[str, Any] = {}
        if name is not None:
            body["name"] = name
        if query is not None:
            body["query"] = query
        if filters is not None:
            body["filters"] = filters
        if enabled is not None:
            body["enabled"] = enabled
        return self._client.request("PUT", f"/v1/pulse/{column_id}", json=body)

    def delete(self, column_id: str) -> Dict[str, Any]:
        """
        DELETE /v1/pulse/{column_id} — delete a pulse column.
        """
        return self._client.request("DELETE", f"/v1/pulse/{column_id}")


class AsyncPulseResource:
    """Asynchronous pulse column methods."""

    def __init__(self, client: "AsyncKronicalClient"):
        self._client = client

    async def list(self) -> Dict[str, Any]:
        """GET /v1/pulse — list all pulse columns."""
        return await self._client.request("GET", "/v1/pulse")

    async def create(
        self,
        name: str,
        query: str,
        *,
        filters: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        """POST /v1/pulse — create a new pulse column."""
        body: Dict[str, Any] = {"name": name, "query": query}
        if filters is not None:
            body["filters"] = filters
        return await self._client.request("POST", "/v1/pulse", json=body)

    async def update(
        self,
        column_id: str,
        *,
        name: Optional[str] = None,
        query: Optional[str] = None,
        filters: Optional[Dict[str, Any]] = None,
        enabled: Optional[bool] = None,
    ) -> Dict[str, Any]:
        """PUT /v1/pulse/{column_id} — update a pulse column."""
        body: Dict[str, Any] = {}
        if name is not None:
            body["name"] = name
        if query is not None:
            body["query"] = query
        if filters is not None:
            body["filters"] = filters
        if enabled is not None:
            body["enabled"] = enabled
        return await self._client.request("PUT", f"/v1/pulse/{column_id}", json=body)

    async def delete(self, column_id: str) -> Dict[str, Any]:
        """DELETE /v1/pulse/{column_id} — delete a pulse column."""
        return await self._client.request("DELETE", f"/v1/pulse/{column_id}")
