"""
Feed/pulse resource — paginated headline access.
"""
from typing import Any, Dict, Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from kronical.client import KronicalClient, AsyncKronicalClient


class FeedResource:
    """Synchronous feed methods."""

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

    def get(
        self,
        *,
        limit: int = 50,
        before: Optional[float] = None,
        after: Optional[float] = None,
        novelty: Optional[str] = None,
        escalation: Optional[str] = None,
        source: Optional[str] = None,
        query: Optional[str] = None,
        days_back: Optional[int] = None,
    ) -> Dict[str, Any]:
        """
        GET /v1/feed — paginated headline feed, with optional semantic search.

        Args:
            limit: Number of headlines to return (max 50).
            before: Return headlines before this Unix timestamp (forward pagination).
            after: Return headlines after this Unix timestamp (polling new).
            novelty: Filter by novelty (new, update, repeat, reversal).
            escalation: Filter by escalation (shift, status-quo).
            source: Filter by source name.
            query: Semantic search query. When provided, returns headlines
                ranked by relevance instead of chronological order.
            days_back: Lookback window in days for search (1-365).
                Only used when query is provided.

        Returns:
            {"headlines": [...], "count": int, "has_more": bool}
        """
        params: Dict[str, Any] = {"limit": limit}
        if before is not None:
            params["before"] = before
        if after is not None:
            params["after"] = after
        if novelty is not None:
            params["novelty"] = novelty
        if escalation is not None:
            params["escalation"] = escalation
        if source is not None:
            params["source"] = source
        if query is not None:
            params["query"] = query
        if days_back is not None:
            params["days_back"] = days_back

        return self._client.request("GET", "/v1/feed", params=params)

    def get_headline(self, headline_id: str) -> Dict[str, Any]:
        """
        GET /v1/feed/headline/{id} — single headline with full context.
        """
        return self._client.request("GET", f"/v1/feed/headline/{headline_id}")


class AsyncFeedResource:
    """Asynchronous feed methods."""

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

    async def get(
        self,
        *,
        limit: int = 50,
        before: Optional[float] = None,
        after: Optional[float] = None,
        novelty: Optional[str] = None,
        escalation: Optional[str] = None,
        source: Optional[str] = None,
        query: Optional[str] = None,
        days_back: Optional[int] = None,
    ) -> Dict[str, Any]:
        """GET /v1/feed — paginated headline feed, with optional semantic search."""
        params: Dict[str, Any] = {"limit": limit}
        if before is not None:
            params["before"] = before
        if after is not None:
            params["after"] = after
        if novelty is not None:
            params["novelty"] = novelty
        if escalation is not None:
            params["escalation"] = escalation
        if source is not None:
            params["source"] = source
        if query is not None:
            params["query"] = query
        if days_back is not None:
            params["days_back"] = days_back

        return await self._client.request("GET", "/v1/feed", params=params)

    async def get_headline(self, headline_id: str) -> Dict[str, Any]:
        """GET /v1/feed/headline/{id} — single headline with full context."""
        return await self._client.request("GET", f"/v1/feed/headline/{headline_id}")
