import logging
from typing import Any, Optional

import requests

from pagerduty_api import log_it
from pagerduty_api.abstract_client import AbstractClient
from pagerduty_api.utils import is_service_name_compliant


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

    @log_it
    def list_services(self, limit: int = 100, params: dict[str, Any] = {}, paginate: bool = True):
        """
        >>> import os
        >>> client = PagerDutyServicesClient(auth_token=os.environ.get('AUTH_TOKEN'))
        >>> services = client.list_services()
        Executing HTTP get on https://api.pagerduty.com/services
        >>> len(services) > 250
        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,
        )

    # def list_compliant_services(
    #     self,
    #     limit: int = 100,
    #     params: dict[str, Any] = {},
    #     paginate: bool = True
    # ):
    #     '''
    #     >>> import os
    #     >>> client = PagerDutyServicesClient(auth_token=os.environ.get('AUTH_TOKEN'))
    #     >>> services = client.list_compliant_services()
    #     Executing HTTP get on https://api.pagerduty.com/services
    #     >>> len(services) > 200
    #     True
    #     '''
    #     services = self.list_services(limit, params, paginate)
    #     return [
    #         s for s in services
    #         if is_service_name_compliant(s['name'])
    #     ]

    def list_compliant_services(self, services: list = [dict[str, Any]]):
        """
        >>> import os
        >>> client = PagerDutyServicesClient(auth_token=os.environ.get('AUTH_TOKEN'))
        >>> services = client.list_compliant_services()
        Executing HTTP get on https://api.pagerduty.com/services
        >>> len(services) > 200
        True
        """
        self._logger.debug("Listing compliant services")
        return [s for s in services if is_service_name_compliant(s["name"])]

        # return service ids
        # return service names
