"""LunarCrush data provider for social metrics."""

from typing import Any

from .base import Provider, ProviderData


class LunarCrushData(ProviderData):
    """LunarCrush specific data."""

    provider: str = "lunarcrush"
    galaxy_score: float | None = None  # 0-100
    alt_rank: int | None = None
    social_volume: int | None = None
    social_volume_change_24h: float | None = None
    social_score: float | None = None
    social_contributors: int | None = None
    social_dominance: float | None = None
    market_dominance: float | None = None
    sentiment: float | None = None  # 0-100
    spam_ratio: float | None = None


class LunarCrushProvider(Provider):
    """LunarCrush API provider for social metrics."""

    name = "lunarcrush"
    base_url = "https://lunarcrush.com/api4/public"
    requires_auth = True

    def _get_auth_headers(self) -> dict[str, str]:
        """Get authentication headers."""
        if self.api_key:
            return {"Authorization": f"Bearer {self.api_key}"}
        return {}

    async def fetch_data(self, symbol: str) -> LunarCrushData:
        """Fetch social metrics from LunarCrush."""
        try:
            # Get coin data
            data = await self.get(
                f"/coins/{symbol.lower()}",
                params={"key": self.api_key},
            )

            coin_data = data.get("data", {})
            if not coin_data:
                return LunarCrushData(symbol=symbol.upper(), raw_data={})

            return LunarCrushData(
                symbol=symbol.upper(),
                galaxy_score=coin_data.get("galaxy_score"),
                alt_rank=coin_data.get("alt_rank"),
                social_volume=coin_data.get("social_volume"),
                social_volume_change_24h=coin_data.get("social_volume_change_24h"),
                social_score=coin_data.get("social_score"),
                social_contributors=coin_data.get("social_contributors"),
                social_dominance=coin_data.get("social_dominance"),
                market_dominance=coin_data.get("market_dominance"),
                sentiment=coin_data.get("sentiment"),
                spam_ratio=coin_data.get("spam_ratio"),
                raw_data=coin_data,
            )

        except Exception:
            return LunarCrushData(symbol=symbol.upper(), raw_data={})

    async def fetch_social_feed(self, symbol: str, limit: int = 50) -> list[dict]:
        """Fetch recent social posts about a coin."""
        try:
            data = await self.get(
                f"/coins/{symbol.lower()}/feeds",
                params={
                    "key": self.api_key,
                    "limit": limit,
                },
            )
            return data.get("data", [])
        except Exception:
            return []

    async def fetch_influencers(self, symbol: str) -> list[dict]:
        """Fetch top influencers talking about a coin."""
        try:
            data = await self.get(
                f"/coins/{symbol.lower()}/influencers",
                params={"key": self.api_key},
            )
            return data.get("data", [])
        except Exception:
            return []
