import logging
from typing import Any, Optional

import requests

from pagerduty_api import log_it
from pagerduty_api.abstract_client import AbstractClient


class PagerDutyAnalyticsClient(AbstractClient):

    def __init__(
        self,
        http_session: requests.Session = requests.Session(),
        endpoint: str = "https://api.pagerduty.com",
        auth_token: Optional[str] = None,
        logger: Optional[logging.Logger] = None,
    ):
        super().__init__(http_session, endpoint, auth_token, logger)
        self._default_url = f"{self._endpoint}/analytics"
        self._default_entity_name = "data"

    @log_it
    def list_aggregated_services(
        self, filters: dict[str, Any] = {}, aggregate_unit: Optional[str] = None
    ):
        """
        >>> import os
        >>> client = PagerDutyAnalyticsClient(auth_token=os.environ.get('AUTH_TOKEN'))
        >>> analytics = client.list_aggregated_services(data=data)
        Executing HTTP get on https://api.pagerduty.com/metrics/incidents/services
        >>> len(analytics) >= 1
        True
        """
        return self.do_analytics_request(
            url=f"{self._default_url}/metrics/incidents/services",
            entity_name=self._default_entity_name,
            filters=filters,
            method="post",
            aggregate_unit=aggregate_unit,
        )

    @log_it
    def list_aggregated_teams(
        self, filters: dict[str, Any] = {}, aggregate_unit: Optional[str] = None
    ):
        """
        >>> import os
        >>> client = PagerDutyAnalyticsClient(auth_token=os.environ.get('AUTH_TOKEN'))
        >>> analytics = client.list_aggregated_teams(data=data)
        Executing HTTP get on https://api.pagerduty.com/metrics/incidents/teams
        >>> len(analytics) >= 1
        True
        """
        return self.do_analytics_request(
            url=f"{self._default_url}/metrics/incidents/teams",
            entity_name=self._default_entity_name,
            filters=filters,
            method="post",
            aggregate_unit=aggregate_unit,
        )

    @log_it
    def list_aggregated_incidents(self, filters: dict[str, Any] = {}):
        """
        >>> import os
        >>> client = PagerDutyAnalyticsClient(auth_token=os.environ.get('AUTH_TOKEN'))
        >>> incidents = client.list_aggregated_incidents(data=data)
        Executing HTTP get on https://api.pagerduty.com/incidents
        >>> len(incidents) >= 1
        True
        """

        return self.do_analytics_request(
            url=f"{self._default_url}/metrics/incidents/all",
            entity_name=self._default_entity_name,
            filters=filters,
            method="post",
        )

    @log_it
    def list_raw_incidents(
        self, filters: dict[str, Any] = None, limit: Optional[int] = None, paginate: bool = False
    ):
        """
        >>> import os
        >>> client = PagerDutyAnalyticsClient(auth_token=os.environ.get('AUTH_TOKEN'))
        >>> incidents = client.list_raw_incidents(data=data)
        Executing HTTP get on https://api.pagerduty.com/analytics/raw/incidents
        >>> len(incidents) >= 1
        True
        """

        return self.do_analytics_request(
            url=f"{self._default_url}/raw/incidents",
            entity_name=self._default_entity_name,
            filters=filters or {},
            method="post",
            limit=limit,
            paginate=paginate,
        )


# ["P6THBO4", "PAOR13M", "PCTB88X", "PYPJAA9", "PG2XNDN", "PQI2LK3", "PNHSHMO", "PEG1GVL", "PRX56PU", "PG65ZZE"]
# feed service ids in batches of 10
# get incident count from here
