import logging
from typing import Any, Optional

import requests

from pagerduty_api import log_it
from pagerduty_api.abstract_client import AbstractClient


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

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