"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .eventsource import EventSource
from .eventtypessortproperty import EventTypesSortProperty
from .listresource_eventtypewithstats_ import (
    ListResourceEventTypeWithStats,
    ListResourceEventTypeWithStatsTypedDict,
)
from polar_sdk.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from polar_sdk.utils import FieldMetadata, QueryParamMetadata
from pydantic import model_serializer
from typing import Callable, List, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


EventTypesListQueryParamOrganizationIDFilterTypedDict = TypeAliasType(
    "EventTypesListQueryParamOrganizationIDFilterTypedDict", Union[str, List[str]]
)
r"""Filter by organization ID."""


EventTypesListQueryParamOrganizationIDFilter = TypeAliasType(
    "EventTypesListQueryParamOrganizationIDFilter", Union[str, List[str]]
)
r"""Filter by organization ID."""


EventTypesListQueryParamCustomerIDFilterTypedDict = TypeAliasType(
    "EventTypesListQueryParamCustomerIDFilterTypedDict", Union[str, List[str]]
)
r"""Filter by customer ID."""


EventTypesListQueryParamCustomerIDFilter = TypeAliasType(
    "EventTypesListQueryParamCustomerIDFilter", Union[str, List[str]]
)
r"""Filter by customer ID."""


EventTypesListQueryParamExternalCustomerIDFilterTypedDict = TypeAliasType(
    "EventTypesListQueryParamExternalCustomerIDFilterTypedDict", Union[str, List[str]]
)
r"""Filter by external customer ID."""


EventTypesListQueryParamExternalCustomerIDFilter = TypeAliasType(
    "EventTypesListQueryParamExternalCustomerIDFilter", Union[str, List[str]]
)
r"""Filter by external customer ID."""


class EventTypesListRequestTypedDict(TypedDict):
    organization_id: NotRequired[
        Nullable[EventTypesListQueryParamOrganizationIDFilterTypedDict]
    ]
    r"""Filter by organization ID."""
    customer_id: NotRequired[
        Nullable[EventTypesListQueryParamCustomerIDFilterTypedDict]
    ]
    r"""Filter by customer ID."""
    external_customer_id: NotRequired[
        Nullable[EventTypesListQueryParamExternalCustomerIDFilterTypedDict]
    ]
    r"""Filter by external customer ID."""
    query: NotRequired[Nullable[str]]
    r"""Query to filter event types by name or label."""
    root_events: NotRequired[bool]
    r"""When true, only return event types with root events (parent_id IS NULL)."""
    parent_id: NotRequired[Nullable[str]]
    r"""Filter by specific parent event ID."""
    source: NotRequired[Nullable[EventSource]]
    r"""Filter by event source (system or user)."""
    page: NotRequired[int]
    r"""Page number, defaults to 1."""
    limit: NotRequired[int]
    r"""Size of a page, defaults to 10. Maximum is 100."""
    sorting: NotRequired[Nullable[List[EventTypesSortProperty]]]
    r"""Sorting criterion. Several criteria can be used simultaneously and will be applied in order. Add a minus sign `-` before the criteria name to sort by descending order."""


class EventTypesListRequest(BaseModel):
    organization_id: Annotated[
        OptionalNullable[EventTypesListQueryParamOrganizationIDFilter],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Filter by organization ID."""

    customer_id: Annotated[
        OptionalNullable[EventTypesListQueryParamCustomerIDFilter],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Filter by customer ID."""

    external_customer_id: Annotated[
        OptionalNullable[EventTypesListQueryParamExternalCustomerIDFilter],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Filter by external customer ID."""

    query: Annotated[
        OptionalNullable[str],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Query to filter event types by name or label."""

    root_events: Annotated[
        Optional[bool],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = False
    r"""When true, only return event types with root events (parent_id IS NULL)."""

    parent_id: Annotated[
        OptionalNullable[str],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Filter by specific parent event ID."""

    source: Annotated[
        OptionalNullable[EventSource],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Filter by event source (system or user)."""

    page: Annotated[
        Optional[int],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = 1
    r"""Page number, defaults to 1."""

    limit: Annotated[
        Optional[int],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = 10
    r"""Size of a page, defaults to 10. Maximum is 100."""

    sorting: Annotated[
        OptionalNullable[List[EventTypesSortProperty]],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Sorting criterion. Several criteria can be used simultaneously and will be applied in order. Add a minus sign `-` before the criteria name to sort by descending order."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = [
            "organization_id",
            "customer_id",
            "external_customer_id",
            "query",
            "root_events",
            "parent_id",
            "source",
            "page",
            "limit",
            "sorting",
        ]
        nullable_fields = [
            "organization_id",
            "customer_id",
            "external_customer_id",
            "query",
            "parent_id",
            "source",
            "sorting",
        ]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m


class EventTypesListResponseTypedDict(TypedDict):
    result: ListResourceEventTypeWithStatsTypedDict


class EventTypesListResponse(BaseModel):
    next: Callable[[], Optional[EventTypesListResponse]]

    result: ListResourceEventTypeWithStats
