from __future__ import annotations

from typing import Any, Dict, Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from socialrails.client import SocialRails, AsyncSocialRails


class Analytics:
    """Synchronous resource for retrieving analytics data."""

    def __init__(self, client: SocialRails) -> None:
        self._client = client

    def get(
        self,
        *,
        platform: Optional[str] = None,
        period: Optional[str] = None,
        post_id: Optional[str] = None,
    ) -> Dict[str, Any]:
        """Get analytics data.

        Args:
            platform: Filter by platform.
            period: Time period (7d, 30d, 90d, 365d). Defaults to 7d.
            post_id: Get analytics for a specific post instead of aggregate data.

        Returns:
            Analytics data including totals and per-platform breakdown,
            or per-post analytics when post_id is provided.
        """
        params: Dict[str, Any] = {}
        if platform is not None:
            params["platform"] = platform
        if period is not None:
            params["period"] = period
        if post_id is not None:
            params["post_id"] = post_id
        return self._client._get("/analytics", params)


class AsyncAnalytics:
    """Asynchronous resource for retrieving analytics data."""

    def __init__(self, client: AsyncSocialRails) -> None:
        self._client = client

    async def get(
        self,
        *,
        platform: Optional[str] = None,
        period: Optional[str] = None,
        post_id: Optional[str] = None,
    ) -> Dict[str, Any]:
        """Get analytics data.

        Args:
            platform: Filter by platform.
            period: Time period (7d, 30d, 90d, 365d). Defaults to 7d.
            post_id: Get analytics for a specific post instead of aggregate data.

        Returns:
            Analytics data including totals and per-platform breakdown,
            or per-post analytics when post_id is provided.
        """
        params: Dict[str, Any] = {}
        if platform is not None:
            params["platform"] = platform
        if period is not None:
            params["period"] = period
        if post_id is not None:
            params["post_id"] = post_id
        return await self._client._get("/analytics", params)
