import logging
import requests

from datetime import datetime, timezone, timedelta
from pagerduty_api import log_it
from pagerduty_api.abstract_client import AbstractClient
from typing import Any, Dict


class PagerDutyChangeEventsClient(AbstractClient):

    def __init__(
        self,
        http_session: requests.Session = requests.Session(),
        endpoint: str = 'https://api.pagerduty.com',
        auth_token: str = None,
        logger: logging.Logger = None
    ):
        super().__init__(http_session, endpoint, auth_token, logger)
        self._default_url = f'{self._endpoint}/change_events'
        self._default_entity_name = 'change_events'
    
    @log_it
    def list_change_events(
        self,
        limit: int = 100,
        since: str = (datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=1)).isoformat(),
        params: Dict[str, Any] = {},
        paginate: bool = True
    ):
        '''
        Links to docs: https://developer.pagerduty.com/api-reference/c36fffe46ba3b-list-change-events
        '''
        if not params:
            params = {
                'query': '',
                'sort_by': 'name',
                'limit': limit,
                'since': since
            }
        params['limit'] = params.get('limit', limit)
        params['since'] = params.get('since', since)
        return self.do_request(
            url=self._default_url,
            entity_name=self._default_entity_name,
            params=params,
            paginate=paginate
        )

    