import requests

from pagerduty_api.abstract_client import AbstractClient
from typing import Any, Dict


class PagerDutyEscalationPoliciesClient(AbstractClient):
    def __init__(
        self,
        http_session: requests.Session = requests.Session(),
        endpoint: str = 'https://api.pagerduty.com',
        auth_token: str = None
    ):
        super().__init__(http_session, endpoint, auth_token)
        self._default_url = f'{self._endpoint}/escalation_policies'
        self._default_entity_name = 'escalation_policies'

    def list_escalation_policies(
        self,
        limit: int = 100,
        params: 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
        )

    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[]': []
            }
        )