import datetime
from http import HTTPStatus
from typing import Any
from uuid import UUID

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.e_suspicious_activity_events_filters_order_column import ESuspiciousActivityEventsFiltersOrderColumn
from ...models.e_suspicious_activity_severity import ESuspiciousActivitySeverity
from ...models.e_suspicious_activity_source_type import ESuspiciousActivitySourceType
from ...models.e_suspicious_activity_state import ESuspiciousActivityState
from ...models.e_suspicious_activity_type import ESuspiciousActivityType
from ...models.error import Error
from ...models.suspicious_activity_events_result import SuspiciousActivityEventsResult
from ...types import UNSET, Response, Unset


def _get_kwargs(
    *,
    skip: int | Unset = UNSET,
    limit: int | Unset = 200,
    order_column: ESuspiciousActivityEventsFiltersOrderColumn | Unset = UNSET,
    order_asc: bool | Unset = UNSET,
    type_filter: list[ESuspiciousActivityType] | Unset = UNSET,
    detected_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    detected_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    backup_object_id_filter: UUID | Unset = UNSET,
    state_filter: list[ESuspiciousActivityState] | Unset = UNSET,
    source_filter: list[ESuspiciousActivitySourceType] | Unset = UNSET,
    severity_filter: list[ESuspiciousActivitySeverity] | Unset = UNSET,
    created_by_filter: str | Unset = UNSET,
    engine_filter: str | Unset = UNSET,
    machine_name_filter: str | Unset = UNSET,
    x_api_version: str = "1.3-rev1",
) -> dict[str, Any]:
    headers: dict[str, Any] = {}
    headers["x-api-version"] = x_api_version

    params: dict[str, Any] = {}

    params["skip"] = skip

    params["limit"] = limit

    json_order_column: str | Unset = UNSET
    if not isinstance(order_column, Unset):
        json_order_column = order_column.value

    params["orderColumn"] = json_order_column

    params["orderAsc"] = order_asc

    json_type_filter: list[str] | Unset = UNSET
    if not isinstance(type_filter, Unset):
        json_type_filter = []
        for type_filter_item_data in type_filter:
            type_filter_item = type_filter_item_data.value
            json_type_filter.append(type_filter_item)

    params["typeFilter"] = json_type_filter

    json_detected_after_time_utc_filter: str | Unset = UNSET
    if not isinstance(detected_after_time_utc_filter, Unset):
        json_detected_after_time_utc_filter = detected_after_time_utc_filter.isoformat()
    params["detectedAfterTimeUtcFilter"] = json_detected_after_time_utc_filter

    json_detected_before_time_utc_filter: str | Unset = UNSET
    if not isinstance(detected_before_time_utc_filter, Unset):
        json_detected_before_time_utc_filter = detected_before_time_utc_filter.isoformat()
    params["detectedBeforeTimeUtcFilter"] = json_detected_before_time_utc_filter

    json_created_after_time_utc_filter: str | Unset = UNSET
    if not isinstance(created_after_time_utc_filter, Unset):
        json_created_after_time_utc_filter = created_after_time_utc_filter.isoformat()
    params["createdAfterTimeUtcFilter"] = json_created_after_time_utc_filter

    json_created_before_time_utc_filter: str | Unset = UNSET
    if not isinstance(created_before_time_utc_filter, Unset):
        json_created_before_time_utc_filter = created_before_time_utc_filter.isoformat()
    params["createdBeforeTimeUtcFilter"] = json_created_before_time_utc_filter

    json_backup_object_id_filter: str | Unset = UNSET
    if not isinstance(backup_object_id_filter, Unset):
        json_backup_object_id_filter = str(backup_object_id_filter)
    params["backupObjectIdFilter"] = json_backup_object_id_filter

    json_state_filter: list[str] | Unset = UNSET
    if not isinstance(state_filter, Unset):
        json_state_filter = []
        for state_filter_item_data in state_filter:
            state_filter_item = state_filter_item_data.value
            json_state_filter.append(state_filter_item)

    params["stateFilter"] = json_state_filter

    json_source_filter: list[str] | Unset = UNSET
    if not isinstance(source_filter, Unset):
        json_source_filter = []
        for source_filter_item_data in source_filter:
            source_filter_item = source_filter_item_data.value
            json_source_filter.append(source_filter_item)

    params["sourceFilter"] = json_source_filter

    json_severity_filter: list[str] | Unset = UNSET
    if not isinstance(severity_filter, Unset):
        json_severity_filter = []
        for severity_filter_item_data in severity_filter:
            severity_filter_item = severity_filter_item_data.value
            json_severity_filter.append(severity_filter_item)

    params["severityFilter"] = json_severity_filter

    params["createdByFilter"] = created_by_filter

    params["engineFilter"] = engine_filter

    params["machineNameFilter"] = machine_name_filter

    params = {k: v for k, v in params.items() if v is not UNSET and v is not None}

    _kwargs: dict[str, Any] = {
        "method": "get",
        "url": "/api/v1/malwareDetection/events",
        "params": params,
    }

    _kwargs["headers"] = headers
    return _kwargs


def _parse_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Error | SuspiciousActivityEventsResult | None:
    if response.status_code == 200:
        response_200 = SuspiciousActivityEventsResult.from_dict(response.json())

        return response_200

    if response.status_code == 401:
        response_401 = Error.from_dict(response.json())

        return response_401

    if response.status_code == 403:
        response_403 = Error.from_dict(response.json())

        return response_403

    if response.status_code == 500:
        response_500 = Error.from_dict(response.json())

        return response_500

    if client.raise_on_unexpected_status:
        raise errors.UnexpectedStatus(response.status_code, response.content)
    else:
        return None


def _build_response(
    *, client: AuthenticatedClient | Client, response: httpx.Response
) -> Response[Error | SuspiciousActivityEventsResult]:
    return Response(
        status_code=HTTPStatus(response.status_code),
        content=response.content,
        headers=response.headers,
        parsed=_parse_response(client=client, response=response),
    )


def sync_detailed(
    *,
    client: AuthenticatedClient | Client,
    skip: int | Unset = UNSET,
    limit: int | Unset = 200,
    order_column: ESuspiciousActivityEventsFiltersOrderColumn | Unset = UNSET,
    order_asc: bool | Unset = UNSET,
    type_filter: list[ESuspiciousActivityType] | Unset = UNSET,
    detected_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    detected_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    backup_object_id_filter: UUID | Unset = UNSET,
    state_filter: list[ESuspiciousActivityState] | Unset = UNSET,
    source_filter: list[ESuspiciousActivitySourceType] | Unset = UNSET,
    severity_filter: list[ESuspiciousActivitySeverity] | Unset = UNSET,
    created_by_filter: str | Unset = UNSET,
    engine_filter: str | Unset = UNSET,
    machine_name_filter: str | Unset = UNSET,
    x_api_version: str = "1.3-rev1",
) -> Response[Error | SuspiciousActivityEventsResult]:
    """Get All Malware Events

     The HTTP GET request to the `/api/v1/malwareDetection/events` endpoint gets an array of all malware
    events created on the backup server.<p>**Available to**&#58; Veeam Backup Administrator, Veeam
    Backup Operator, Veeam Restore Operator, Veeam Tape Operator, Veeam Backup Viewer, Veeam Security
    Administrator, Incident API Operator.</p>

    Args:
        skip (int | Unset):
        limit (int | Unset):  Default: 200.
        order_column (ESuspiciousActivityEventsFiltersOrderColumn | Unset):
            ESuspiciousActivityEventsFiltersOrderColumn.
        order_asc (bool | Unset):
        type_filter (list[ESuspiciousActivityType] | Unset):
        detected_after_time_utc_filter (datetime.datetime | Unset):
        detected_before_time_utc_filter (datetime.datetime | Unset):
        created_after_time_utc_filter (datetime.datetime | Unset):
        created_before_time_utc_filter (datetime.datetime | Unset):
        backup_object_id_filter (UUID | Unset):
        state_filter (list[ESuspiciousActivityState] | Unset):
        source_filter (list[ESuspiciousActivitySourceType] | Unset):
        severity_filter (list[ESuspiciousActivitySeverity] | Unset):
        created_by_filter (str | Unset):
        engine_filter (str | Unset):
        machine_name_filter (str | Unset):
        x_api_version (str):  Default: '1.3-rev1'.

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[Error | SuspiciousActivityEventsResult]
    """

    kwargs = _get_kwargs(
        skip=skip,
        limit=limit,
        order_column=order_column,
        order_asc=order_asc,
        type_filter=type_filter,
        detected_after_time_utc_filter=detected_after_time_utc_filter,
        detected_before_time_utc_filter=detected_before_time_utc_filter,
        created_after_time_utc_filter=created_after_time_utc_filter,
        created_before_time_utc_filter=created_before_time_utc_filter,
        backup_object_id_filter=backup_object_id_filter,
        state_filter=state_filter,
        source_filter=source_filter,
        severity_filter=severity_filter,
        created_by_filter=created_by_filter,
        engine_filter=engine_filter,
        machine_name_filter=machine_name_filter,
        x_api_version=x_api_version,
    )

    response = client.get_httpx_client().request(
        **kwargs,
    )

    return _build_response(client=client, response=response)


def sync(
    *,
    client: AuthenticatedClient | Client,
    skip: int | Unset = UNSET,
    limit: int | Unset = 200,
    order_column: ESuspiciousActivityEventsFiltersOrderColumn | Unset = UNSET,
    order_asc: bool | Unset = UNSET,
    type_filter: list[ESuspiciousActivityType] | Unset = UNSET,
    detected_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    detected_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    backup_object_id_filter: UUID | Unset = UNSET,
    state_filter: list[ESuspiciousActivityState] | Unset = UNSET,
    source_filter: list[ESuspiciousActivitySourceType] | Unset = UNSET,
    severity_filter: list[ESuspiciousActivitySeverity] | Unset = UNSET,
    created_by_filter: str | Unset = UNSET,
    engine_filter: str | Unset = UNSET,
    machine_name_filter: str | Unset = UNSET,
    x_api_version: str = "1.3-rev1",
) -> Error | SuspiciousActivityEventsResult | None:
    """Get All Malware Events

     The HTTP GET request to the `/api/v1/malwareDetection/events` endpoint gets an array of all malware
    events created on the backup server.<p>**Available to**&#58; Veeam Backup Administrator, Veeam
    Backup Operator, Veeam Restore Operator, Veeam Tape Operator, Veeam Backup Viewer, Veeam Security
    Administrator, Incident API Operator.</p>

    Args:
        skip (int | Unset):
        limit (int | Unset):  Default: 200.
        order_column (ESuspiciousActivityEventsFiltersOrderColumn | Unset):
            ESuspiciousActivityEventsFiltersOrderColumn.
        order_asc (bool | Unset):
        type_filter (list[ESuspiciousActivityType] | Unset):
        detected_after_time_utc_filter (datetime.datetime | Unset):
        detected_before_time_utc_filter (datetime.datetime | Unset):
        created_after_time_utc_filter (datetime.datetime | Unset):
        created_before_time_utc_filter (datetime.datetime | Unset):
        backup_object_id_filter (UUID | Unset):
        state_filter (list[ESuspiciousActivityState] | Unset):
        source_filter (list[ESuspiciousActivitySourceType] | Unset):
        severity_filter (list[ESuspiciousActivitySeverity] | Unset):
        created_by_filter (str | Unset):
        engine_filter (str | Unset):
        machine_name_filter (str | Unset):
        x_api_version (str):  Default: '1.3-rev1'.

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Error | SuspiciousActivityEventsResult
    """

    return sync_detailed(
        client=client,
        skip=skip,
        limit=limit,
        order_column=order_column,
        order_asc=order_asc,
        type_filter=type_filter,
        detected_after_time_utc_filter=detected_after_time_utc_filter,
        detected_before_time_utc_filter=detected_before_time_utc_filter,
        created_after_time_utc_filter=created_after_time_utc_filter,
        created_before_time_utc_filter=created_before_time_utc_filter,
        backup_object_id_filter=backup_object_id_filter,
        state_filter=state_filter,
        source_filter=source_filter,
        severity_filter=severity_filter,
        created_by_filter=created_by_filter,
        engine_filter=engine_filter,
        machine_name_filter=machine_name_filter,
        x_api_version=x_api_version,
    ).parsed


async def asyncio_detailed(
    *,
    client: AuthenticatedClient | Client,
    skip: int | Unset = UNSET,
    limit: int | Unset = 200,
    order_column: ESuspiciousActivityEventsFiltersOrderColumn | Unset = UNSET,
    order_asc: bool | Unset = UNSET,
    type_filter: list[ESuspiciousActivityType] | Unset = UNSET,
    detected_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    detected_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    backup_object_id_filter: UUID | Unset = UNSET,
    state_filter: list[ESuspiciousActivityState] | Unset = UNSET,
    source_filter: list[ESuspiciousActivitySourceType] | Unset = UNSET,
    severity_filter: list[ESuspiciousActivitySeverity] | Unset = UNSET,
    created_by_filter: str | Unset = UNSET,
    engine_filter: str | Unset = UNSET,
    machine_name_filter: str | Unset = UNSET,
    x_api_version: str = "1.3-rev1",
) -> Response[Error | SuspiciousActivityEventsResult]:
    """Get All Malware Events

     The HTTP GET request to the `/api/v1/malwareDetection/events` endpoint gets an array of all malware
    events created on the backup server.<p>**Available to**&#58; Veeam Backup Administrator, Veeam
    Backup Operator, Veeam Restore Operator, Veeam Tape Operator, Veeam Backup Viewer, Veeam Security
    Administrator, Incident API Operator.</p>

    Args:
        skip (int | Unset):
        limit (int | Unset):  Default: 200.
        order_column (ESuspiciousActivityEventsFiltersOrderColumn | Unset):
            ESuspiciousActivityEventsFiltersOrderColumn.
        order_asc (bool | Unset):
        type_filter (list[ESuspiciousActivityType] | Unset):
        detected_after_time_utc_filter (datetime.datetime | Unset):
        detected_before_time_utc_filter (datetime.datetime | Unset):
        created_after_time_utc_filter (datetime.datetime | Unset):
        created_before_time_utc_filter (datetime.datetime | Unset):
        backup_object_id_filter (UUID | Unset):
        state_filter (list[ESuspiciousActivityState] | Unset):
        source_filter (list[ESuspiciousActivitySourceType] | Unset):
        severity_filter (list[ESuspiciousActivitySeverity] | Unset):
        created_by_filter (str | Unset):
        engine_filter (str | Unset):
        machine_name_filter (str | Unset):
        x_api_version (str):  Default: '1.3-rev1'.

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Response[Error | SuspiciousActivityEventsResult]
    """

    kwargs = _get_kwargs(
        skip=skip,
        limit=limit,
        order_column=order_column,
        order_asc=order_asc,
        type_filter=type_filter,
        detected_after_time_utc_filter=detected_after_time_utc_filter,
        detected_before_time_utc_filter=detected_before_time_utc_filter,
        created_after_time_utc_filter=created_after_time_utc_filter,
        created_before_time_utc_filter=created_before_time_utc_filter,
        backup_object_id_filter=backup_object_id_filter,
        state_filter=state_filter,
        source_filter=source_filter,
        severity_filter=severity_filter,
        created_by_filter=created_by_filter,
        engine_filter=engine_filter,
        machine_name_filter=machine_name_filter,
        x_api_version=x_api_version,
    )

    response = await client.get_async_httpx_client().request(**kwargs)

    return _build_response(client=client, response=response)


async def asyncio(
    *,
    client: AuthenticatedClient | Client,
    skip: int | Unset = UNSET,
    limit: int | Unset = 200,
    order_column: ESuspiciousActivityEventsFiltersOrderColumn | Unset = UNSET,
    order_asc: bool | Unset = UNSET,
    type_filter: list[ESuspiciousActivityType] | Unset = UNSET,
    detected_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    detected_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_after_time_utc_filter: datetime.datetime | Unset = UNSET,
    created_before_time_utc_filter: datetime.datetime | Unset = UNSET,
    backup_object_id_filter: UUID | Unset = UNSET,
    state_filter: list[ESuspiciousActivityState] | Unset = UNSET,
    source_filter: list[ESuspiciousActivitySourceType] | Unset = UNSET,
    severity_filter: list[ESuspiciousActivitySeverity] | Unset = UNSET,
    created_by_filter: str | Unset = UNSET,
    engine_filter: str | Unset = UNSET,
    machine_name_filter: str | Unset = UNSET,
    x_api_version: str = "1.3-rev1",
) -> Error | SuspiciousActivityEventsResult | None:
    """Get All Malware Events

     The HTTP GET request to the `/api/v1/malwareDetection/events` endpoint gets an array of all malware
    events created on the backup server.<p>**Available to**&#58; Veeam Backup Administrator, Veeam
    Backup Operator, Veeam Restore Operator, Veeam Tape Operator, Veeam Backup Viewer, Veeam Security
    Administrator, Incident API Operator.</p>

    Args:
        skip (int | Unset):
        limit (int | Unset):  Default: 200.
        order_column (ESuspiciousActivityEventsFiltersOrderColumn | Unset):
            ESuspiciousActivityEventsFiltersOrderColumn.
        order_asc (bool | Unset):
        type_filter (list[ESuspiciousActivityType] | Unset):
        detected_after_time_utc_filter (datetime.datetime | Unset):
        detected_before_time_utc_filter (datetime.datetime | Unset):
        created_after_time_utc_filter (datetime.datetime | Unset):
        created_before_time_utc_filter (datetime.datetime | Unset):
        backup_object_id_filter (UUID | Unset):
        state_filter (list[ESuspiciousActivityState] | Unset):
        source_filter (list[ESuspiciousActivitySourceType] | Unset):
        severity_filter (list[ESuspiciousActivitySeverity] | Unset):
        created_by_filter (str | Unset):
        engine_filter (str | Unset):
        machine_name_filter (str | Unset):
        x_api_version (str):  Default: '1.3-rev1'.

    Raises:
        errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
        httpx.TimeoutException: If the request takes longer than Client.timeout.

    Returns:
        Error | SuspiciousActivityEventsResult
    """

    return (
        await asyncio_detailed(
            client=client,
            skip=skip,
            limit=limit,
            order_column=order_column,
            order_asc=order_asc,
            type_filter=type_filter,
            detected_after_time_utc_filter=detected_after_time_utc_filter,
            detected_before_time_utc_filter=detected_before_time_utc_filter,
            created_after_time_utc_filter=created_after_time_utc_filter,
            created_before_time_utc_filter=created_before_time_utc_filter,
            backup_object_id_filter=backup_object_id_filter,
            state_filter=state_filter,
            source_filter=source_filter,
            severity_filter=severity_filter,
            created_by_filter=created_by_filter,
            engine_filter=engine_filter,
            machine_name_filter=machine_name_filter,
            x_api_version=x_api_version,
        )
    ).parsed
