"""
Enrichment resource — single submit, batch, and polling.
"""
from typing import Any, Dict, List, Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from kronical.client import KronicalClient, AsyncKronicalClient


class EnrichmentResource:
    """Synchronous enrichment methods."""

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

    def submit(
        self,
        headline_text: str,
        *,
        idempotency_key: Optional[str] = None,
    ) -> Dict[str, Any]:
        """
        POST /v1/enrichment/submit — enrich a single headline.

        Args:
            headline_text: The headline text to enrich (min 5 chars).
            idempotency_key: Optional key to prevent duplicate processing.

        Returns:
            {"headline_text": str, "enrichment": {...}}
        """
        headers = {}
        if idempotency_key:
            headers["Idempotency-Key"] = idempotency_key
        return self._client.request(
            "POST", "/v1/enrichment/submit",
            json={"headline_text": headline_text},
            headers=headers or None,
        )

    def batch(
        self,
        headlines: List[str],
        *,
        idempotency_key: Optional[str] = None,
    ) -> Dict[str, Any]:
        """
        POST /v1/enrichment/batch — submit a batch for async enrichment.

        Args:
            headlines: List of headline texts.
            idempotency_key: Optional key to prevent duplicate batches.

        Returns:
            {"batch_id": str, "total_items": int, "status": str}
        """
        headers = {}
        if idempotency_key:
            headers["Idempotency-Key"] = idempotency_key
        return self._client.request(
            "POST", "/v1/enrichment/batch",
            json={"headlines": headlines},
            headers=headers or None,
        )

    def get_batch(self, batch_id: str) -> Dict[str, Any]:
        """
        GET /v1/enrichment/batch/{batch_id} — poll batch results.

        Returns:
            {"batch": {...}, "items": [...]}
        """
        return self._client.request("GET", f"/v1/enrichment/batch/{batch_id}")


class AsyncEnrichmentResource:
    """Asynchronous enrichment methods."""

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

    async def submit(
        self,
        headline_text: str,
        *,
        idempotency_key: Optional[str] = None,
    ) -> Dict[str, Any]:
        """POST /v1/enrichment/submit — enrich a single headline."""
        headers = {}
        if idempotency_key:
            headers["Idempotency-Key"] = idempotency_key
        return await self._client.request(
            "POST", "/v1/enrichment/submit",
            json={"headline_text": headline_text},
            headers=headers or None,
        )

    async def batch(
        self,
        headlines: List[str],
        *,
        idempotency_key: Optional[str] = None,
    ) -> Dict[str, Any]:
        """POST /v1/enrichment/batch — submit a batch for async enrichment."""
        headers = {}
        if idempotency_key:
            headers["Idempotency-Key"] = idempotency_key
        return await self._client.request(
            "POST", "/v1/enrichment/batch",
            json={"headlines": headlines},
            headers=headers or None,
        )

    async def get_batch(self, batch_id: str) -> Dict[str, Any]:
        """GET /v1/enrichment/batch/{batch_id} — poll batch results."""
        return await self._client.request("GET", f"/v1/enrichment/batch/{batch_id}")
