"""Insight API endpoints.

This file is automatically @generated from the Meraki API specification.
Do not edit this file manually.

"""

from __future__ import annotations

import urllib.parse
from typing import TYPE_CHECKING, Any

from meraki_client.schemas import (
    CreateOrganizationInsightMonitoredMediaServerResponse,
    GetNetworkInsightApplicationHealthByTimeResponseItem,
    GetOrganizationInsightApplicationsResponseItem,
    GetOrganizationInsightMonitoredMediaServerResponse,
    GetOrganizationInsightMonitoredMediaServersResponseItem,
    UpdateOrganizationInsightMonitoredMediaServerResponse,
)

if TYPE_CHECKING:
    from meraki_client._session import PaginatedResponse, Session


class Insight:
    """Insight class."""

    def __init__(self, session: Session) -> None:
        self._session = session

    def get_network_insight_application_health_by_time(
        self,
        *,
        network_id: str,
        application_id: str,
        t0: str | None = None,
        t1: str | None = None,
        timespan: float | None = None,
        resolution: int | None = None,
    ) -> PaginatedResponse[GetNetworkInsightApplicationHealthByTimeResponseItem]:
        """Get application health by time.

        [API documentation: getNetworkInsightApplicationHealthByTime](https://developer.cisco.com/meraki/api-v1/#!get-network-insight-application-health-by-time)

        Args:
            network_id: Network ID.
            application_id: Application ID.
            t0: The beginning of the timespan for the data. The maximum lookback period is 7 days
                from today.
            t1: The end of the timespan for the data. t1 can be a maximum of 7 days after t0.
            timespan: The timespan for which the information will be fetched. If specifying
                timespan, do not specify parameters t0 and t1. The value must be in
                seconds and be less than or equal to 7 days. The default is 2 hours.
            resolution: The time resolution in seconds for returned data. The valid resolutions are:
                60, 300, 3600, 86400. The default is 300.

        Returns:
            Successful operation.

        Note:
            Returns a lazy PaginatedResponse
            that can be iterated or collected with `.collect()`.
            Page metadata is available on `.meta` and `.meta_pages`.

        Example API response:
            ```json
            [
              {
                "startTs": "2018-02-11T00:00:00Z",
                "endTs": "2018-05-12T00:00:00Z",
                "wanGoodput": 20000000,
                "lanGoodput": 100000000,
                "wanLatencyMs": 10.1,
                "lanLatencyMs": 3.2,
                "wanLossPercent": 0.2,
                "lanLossPercent": 0.0,
                "responseDuration": 210,
                "sent": 1000,
                "recv": 5000,
                "numClients": 2
              }
            ]
            ```

        """
        network_id = urllib.parse.quote(str(network_id), safe="")
        application_id = urllib.parse.quote(str(application_id), safe="")
        path = f"/networks/{network_id}/insight/applications/{application_id}/healthByTime"

        params: dict[str, Any] = {}
        if t0 is not None:
            params["t0"] = t0
        if t1 is not None:
            params["t1"] = t1
        if timespan is not None:
            params["timespan"] = timespan
        if resolution is not None:
            params["resolution"] = resolution

        return self._session.get_pages(
            scope="insight",
            operation_id="getNetworkInsightApplicationHealthByTime",
            path=path,
            params=params,
            item_schema=GetNetworkInsightApplicationHealthByTimeResponseItem,
        )

    def get_organization_insight_applications(
        self, organization_id: str
    ) -> PaginatedResponse[GetOrganizationInsightApplicationsResponseItem]:
        """List all Insight tracked applications.

        [API documentation: getOrganizationInsightApplications](https://developer.cisco.com/meraki/api-v1/#!get-organization-insight-applications)

        Args:
            organization_id: Organization ID.

        Returns:
            Successful operation.

        Note:
            Returns a lazy PaginatedResponse
            that can be iterated or collected with `.collect()`.
            Page metadata is available on `.meta` and `.meta_pages`.

        Example API response:
            ```json
            [
              {
                "applicationId": "19.12",
                "name": "Meraki HTTPS",
                "thresholds": {
                  "type": "smart",
                  "byNetwork": [
                    {
                      "networkId": "N_12345678",
                      "goodput": 50000,
                      "responseDuration": 1000
                    }
                  ]
                }
              }
            ]
            ```

        """
        organization_id = urllib.parse.quote(str(organization_id), safe="")
        path = f"/organizations/{organization_id}/insight/applications"

        return self._session.get_pages(
            scope="insight",
            operation_id="getOrganizationInsightApplications",
            path=path,
            item_schema=GetOrganizationInsightApplicationsResponseItem,
        )

    def get_organization_insight_monitored_media_servers(
        self, organization_id: str
    ) -> PaginatedResponse[GetOrganizationInsightMonitoredMediaServersResponseItem]:
        """List the monitored media servers for this organization.

        [API documentation: getOrganizationInsightMonitoredMediaServers](https://developer.cisco.com/meraki/api-v1/#!get-organization-insight-monitored-media-servers)

        Args:
            organization_id: Organization ID.

        Returns:
            Successful operation.

        Note:
            Returns a lazy PaginatedResponse
            that can be iterated or collected with `.collect()`.
            Page metadata is available on `.meta` and `.meta_pages`.

        Example API response:
            ```json
            [
              {
                "id": "1284392014819",
                "name": "Sample VoIP Provider",
                "address": "123.123.123.1",
                "bestEffortMonitoringEnabled": true
              }
            ]
            ```

        """
        organization_id = urllib.parse.quote(str(organization_id), safe="")
        path = f"/organizations/{organization_id}/insight/monitoredMediaServers"

        return self._session.get_pages(
            scope="insight",
            operation_id="getOrganizationInsightMonitoredMediaServers",
            path=path,
            item_schema=GetOrganizationInsightMonitoredMediaServersResponseItem,
        )

    def create_organization_insight_monitored_media_server(
        self,
        *,
        organization_id: str,
        name: str,
        address: str,
        best_effort_monitoring_enabled: bool | None = None,
    ) -> CreateOrganizationInsightMonitoredMediaServerResponse:
        """Add a media server to be monitored for this organization.

        [API documentation: createOrganizationInsightMonitoredMediaServer](https://developer.cisco.com/meraki/api-v1/#!create-organization-insight-monitored-media-server)

        Args:
            organization_id: Organization ID.
            name: The name of the VoIP provider.
            address: The IP address (IPv4 only) or hostname of the media server to monitor.
            best_effort_monitoring_enabled: Indicates that if the media server doesn't respond to
                ICMP pings, the nearest hop will be used in its stead.

        Returns:
            Successful operation.

        Example API response:
            ```json
            {
              "id": "1284392014819",
              "name": "Sample VoIP Provider",
              "address": "123.123.123.1",
              "bestEffortMonitoringEnabled": true
            }
            ```

        """
        organization_id = urllib.parse.quote(str(organization_id), safe="")
        path = f"/organizations/{organization_id}/insight/monitoredMediaServers"

        payload: dict[str, Any] = {}
        if name is not None:
            payload["name"] = name
        if address is not None:
            payload["address"] = address
        if best_effort_monitoring_enabled is not None:
            payload["bestEffortMonitoringEnabled"] = best_effort_monitoring_enabled

        return self._session.post(
            scope="insight",
            operation_id="createOrganizationInsightMonitoredMediaServer",
            path=path,
            json=payload,
            response_schema=CreateOrganizationInsightMonitoredMediaServerResponse,
        )

    def get_organization_insight_monitored_media_server(
        self, *, organization_id: str, monitored_media_server_id: str
    ) -> GetOrganizationInsightMonitoredMediaServerResponse:
        """Return a monitored media server for this organization.

        [API documentation: getOrganizationInsightMonitoredMediaServer](https://developer.cisco.com/meraki/api-v1/#!get-organization-insight-monitored-media-server)

        Args:
            organization_id: Organization ID.
            monitored_media_server_id: Monitored media server ID.

        Returns:
            Successful operation.

        Example API response:
            ```json
            {
              "id": "1284392014819",
              "name": "Sample VoIP Provider",
              "address": "123.123.123.1",
              "bestEffortMonitoringEnabled": true
            }
            ```

        """
        organization_id = urllib.parse.quote(str(organization_id), safe="")
        monitored_media_server_id = urllib.parse.quote(str(monitored_media_server_id), safe="")
        path = f"/organizations/{organization_id}/insight/monitoredMediaServers/{monitored_media_server_id}"

        return self._session.get(
            scope="insight",
            operation_id="getOrganizationInsightMonitoredMediaServer",
            path=path,
            response_schema=GetOrganizationInsightMonitoredMediaServerResponse,
        )

    def update_organization_insight_monitored_media_server(
        self,
        *,
        organization_id: str,
        monitored_media_server_id: str,
        name: str | None = None,
        address: str | None = None,
        best_effort_monitoring_enabled: bool | None = None,
    ) -> UpdateOrganizationInsightMonitoredMediaServerResponse:
        """Update a monitored media server for this organization.

        [API documentation: updateOrganizationInsightMonitoredMediaServer](https://developer.cisco.com/meraki/api-v1/#!update-organization-insight-monitored-media-server)

        Args:
            organization_id: Organization ID.
            monitored_media_server_id: Monitored media server ID.
            name: The name of the VoIP provider.
            address: The IP address (IPv4 only) or hostname of the media server to monitor.
            best_effort_monitoring_enabled: Indicates that if the media server doesn't respond to
                ICMP pings, the nearest hop will be used in its stead.

        Returns:
            Successful operation.

        Example API response:
            ```json
            {
              "id": "1284392014819",
              "name": "Sample VoIP Provider",
              "address": "123.123.123.1",
              "bestEffortMonitoringEnabled": true
            }
            ```

        """
        organization_id = urllib.parse.quote(str(organization_id), safe="")
        monitored_media_server_id = urllib.parse.quote(str(monitored_media_server_id), safe="")
        path = f"/organizations/{organization_id}/insight/monitoredMediaServers/{monitored_media_server_id}"

        payload: dict[str, Any] = {}
        if name is not None:
            payload["name"] = name
        if address is not None:
            payload["address"] = address
        if best_effort_monitoring_enabled is not None:
            payload["bestEffortMonitoringEnabled"] = best_effort_monitoring_enabled

        return self._session.put(
            scope="insight",
            operation_id="updateOrganizationInsightMonitoredMediaServer",
            path=path,
            json=payload,
            response_schema=UpdateOrganizationInsightMonitoredMediaServerResponse,
        )

    def delete_organization_insight_monitored_media_server(
        self, *, organization_id: str, monitored_media_server_id: str
    ) -> None:
        """Delete a monitored media server from this organization.

        [API documentation: deleteOrganizationInsightMonitoredMediaServer](https://developer.cisco.com/meraki/api-v1/#!delete-organization-insight-monitored-media-server)

        Args:
            organization_id: Organization ID.
            monitored_media_server_id: Monitored media server ID.

        Returns:
            Successful operation.

        """
        organization_id = urllib.parse.quote(str(organization_id), safe="")
        monitored_media_server_id = urllib.parse.quote(str(monitored_media_server_id), safe="")
        path = f"/organizations/{organization_id}/insight/monitoredMediaServers/{monitored_media_server_id}"

        return self._session.delete(
            scope="insight", operation_id="deleteOrganizationInsightMonitoredMediaServer", path=path
        )
