"""
Smart columns resource — CRUD and match retrieval.
"""
from typing import Any, Dict, Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from kronical.client import KronicalClient, AsyncKronicalClient


class SmartColumnsResource:
    """Synchronous smart columns methods."""

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

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

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

    def create(self, name: str, instruction: str) -> Dict[str, Any]:
        """
        POST /v1/smart-columns — create a smart column.

        Args:
            name: Display name (1-100 chars).
            instruction: Natural language matching instruction (5-1000 chars).

        Returns:
            {"column_id": str, "name": str, "instruction": str, "enabled": bool, ...}
        """
        return self._client.request("POST", "/v1/smart-columns", json={
            "name": name,
            "instruction": instruction,
        })

    def delete(self, column_id: str) -> Dict[str, Any]:
        """
        DELETE /v1/smart-columns/{column_id} — delete a smart column.

        Returns:
            {"status": "deleted", "column_id": str}
        """
        return self._client.request("DELETE", f"/v1/smart-columns/{column_id}")

    def matches(self, *, limit: int = 50, hours: int = 24) -> Dict[str, Any]:
        """
        GET /v1/smart-columns/matches — recent matches.

        Args:
            limit: Max matches to return.
            hours: Lookback window (1-168).

        Returns:
            {"matches": [...], "count": int}
        """
        return self._client.request("GET", "/v1/smart-columns/matches", params={
            "limit": limit,
            "hours": hours,
        })

    def evaluate(
        self,
        headline_text: str,
        instruction: str,
        *,
        context: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        """
        POST /v1/smart-columns/evaluate — preview a headline against an instruction.
        """
        body: Dict[str, Any] = {
            "headline_text": headline_text,
            "instruction": instruction,
        }
        if context is not None:
            body["context"] = context
        return self._client.request("POST", "/v1/smart-columns/evaluate", json=body)


class AsyncSmartColumnsResource:
    """Asynchronous smart columns methods."""

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

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

    async def create(self, name: str, instruction: str) -> Dict[str, Any]:
        """POST /v1/smart-columns — create a smart column."""
        return await self._client.request("POST", "/v1/smart-columns", json={
            "name": name,
            "instruction": instruction,
        })

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

    async def matches(self, *, limit: int = 50, hours: int = 24) -> Dict[str, Any]:
        """GET /v1/smart-columns/matches — recent matches."""
        return await self._client.request("GET", "/v1/smart-columns/matches", params={
            "limit": limit,
            "hours": hours,
        })

    async def evaluate(
        self,
        headline_text: str,
        instruction: str,
        *,
        context: Optional[Dict[str, Any]] = None,
    ) -> Dict[str, Any]:
        """POST /v1/smart-columns/evaluate — preview evaluation."""
        body: Dict[str, Any] = {
            "headline_text": headline_text,
            "instruction": instruction,
        }
        if context is not None:
            body["context"] = context
        return await self._client.request("POST", "/v1/smart-columns/evaluate", json=body)
