"""Macro news adapter for global/geopolitical news ingestion.

Fetches macro-level news articles from configured sources for global event
classification. Reuses the same adapter pattern as company-specific news
but targets macro-focused endpoints and does not require a ticker.

Requirements: 1.1, 1.2, 1.3, 1.4
"""
import hashlib
import logging
import time
from datetime import datetime, timezone
from typing import Any

import httpx

from .base import AdapterResult, BaseAdapter

logger = logging.getLogger("macro_news_adapter")


class MacroNewsAdapter(BaseAdapter):
    """Adapter for fetching macro/geopolitical news from configured sources.

    Supports fetching from any HTTP endpoint that returns JSON with a list
    of news articles. The endpoint URL and response parsing are configured
    via the source config dict.

    Config options:
        url: The endpoint URL to fetch from
        limit: Max articles to return per request (default 20)
        params: Additional query parameters as a dict
        results_key: JSON key containing the article list (default "results")
    """

    def __init__(self, api_key: str = "", base_url: str = "") -> None:
        self.api_key = api_key
        self.base_url = base_url.rstrip("/") if base_url else ""

    def source_type(self) -> str:
        return "macro_news"

    async def fetch(self, ticker: str, config: dict[str, Any]) -> AdapterResult:
        """Fetch macro news articles from the configured endpoint.

        The ticker parameter is ignored for macro sources — these are
        global/geopolitical news, not company-specific.

        Uses published_utc.gt to only fetch articles newer than the last
        successful fetch, avoiding re-fetching the same articles.

        Args:
            ticker: Ignored for macro sources (may be empty string).
            config: Source-specific configuration with url, params, etc.

        Returns:
            AdapterResult with raw payload and parsed article items.
        """
        url = config.get("url", "")
        if not url and self.base_url:
            url = self.base_url

        if not url:
            return self._error_result("No URL configured for macro news source")

        params = dict(config.get("params", {}))
        if self.api_key:
            params["apiKey"] = self.api_key

        limit = config.get("limit", 20)
        params["limit"] = str(min(int(limit), 1000))

        # Use last_published_at from config to only fetch newer articles
        last_published = config.get("last_published_at")
        if last_published:
            params["published_utc.gt"] = last_published

        async with httpx.AsyncClient(timeout=30) as client:
            t0 = time.monotonic()
            try:
                resp = await client.get(url, params=params)
                elapsed_ms = (time.monotonic() - t0) * 1000
                resp.raise_for_status()

                raw = resp.content
                data = resp.json()
                content_hash = hashlib.sha256(raw).hexdigest()

                results_key = config.get("results_key", "results")
                items = data.get(results_key, [])
                if not isinstance(items, list):
                    items = []

                return AdapterResult(
                    source_type="macro_news",
                    ticker="",
                    items=items,
                    raw_payload=raw,
                    content_hash=content_hash,
                    fetched_at=datetime.now(timezone.utc),
                    http_status=resp.status_code,
                    response_time_ms=round(elapsed_ms, 1),
                    metadata={
                        "provider": config.get("provider", "macro"),
                        "results_count": len(items),
                    },
                )
            except httpx.HTTPStatusError as e:
                elapsed_ms = (time.monotonic() - t0) * 1000
                logger.error("Macro news HTTP error: %s", e)
                return self._error_result(
                    str(e), elapsed_ms,
                    http_status=e.response.status_code if e.response else None,
                    raw=e.response.content if e.response else b"",
                )
            except httpx.TimeoutException as e:
                elapsed_ms = (time.monotonic() - t0) * 1000
                logger.error("Macro news timeout: %s", e)
                return self._error_result(f"timeout: {e}", elapsed_ms)
            except Exception as e:
                elapsed_ms = (time.monotonic() - t0) * 1000
                logger.error("Macro news fetch failed: %s", e)
                return self._error_result(str(e), elapsed_ms)

    def _error_result(
        self,
        error: str,
        elapsed_ms: float = 0.0,
        http_status: int | None = None,
        raw: bytes = b"",
    ) -> AdapterResult:
        """Build an error AdapterResult for macro news fetches."""
        return AdapterResult(
            source_type="macro_news",
            ticker="",
            items=[],
            raw_payload=raw,
            content_hash="",
            fetched_at=datetime.now(timezone.utc),
            error=error,
            http_status=http_status,
            response_time_ms=round(elapsed_ms, 1),
            metadata={"provider": "macro"},
        )
