import logging
from typing import Any, Optional

import requests

from pagerduty_api import log_it
from pagerduty_api.abstract_client import AbstractClient


class PagerDutyEscalationPoliciesClient(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}/escalation_policies"
        self._default_entity_name = "escalation_policies"

    @log_it
    def list_escalation_policies(
        self, limit: int = 100, params: Optional[dict[str, Any]] = None, paginate: bool = True
    ):
        """
        >>> import os
        >>> client = PagerDutyEscalationPoliciesClient(auth_token=os.environ.get('AUTH_TOKEN'))
        >>> policies = client.list_escalation_policies()
        Executing HTTP get on https://api.pagerduty.com/escalation_policies
        >>> len(policies) > 50
        True
        """
        if not params:
            params = {
                "query": "",
                "sort_by": "name",
                "limit": limit,
            }
        return self.do_request(
            url=self._default_url,
            entity_name=self._default_entity_name,
            params=params,
            paginate=paginate,
        )

    @log_it
    def get_escalation_policies_by_name(self, policy_name: str):
        """
        >>> import os
        >>> client = PagerDutyEscalationPoliciesClient(auth_token=os.environ.get('AUTH_TOKEN'))
        >>> policies = client.get_escalation_policies_by_name('AISO-Tech')
        Executing HTTP get on https://api.pagerduty.com/escalation_policies
        >>> len(policies) == 1
        True
        """
        return self.do_request(
            url=self._default_url,
            entity_name=self._default_entity_name,
            params={
                "user_ids[]": [],
                "time_zone": "UTC",
                "sort_by": "name",
                "query": str(policy_name),
                "include[]": [],
            },
        )
