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

from __future__ import annotations
from attio.types import BaseModel, Nullable, UNSET_SENTINEL
from attio.utils import FieldMetadata, PathParamMetadata, RequestMetadata
import pydantic
from pydantic import model_serializer
from typing import List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


PatchV2WebhooksWebhookIDEventTypeRequest = Literal[
    "call-recording.created",
    "comment.created",
    "comment.resolved",
    "comment.unresolved",
    "comment.deleted",
    "list.created",
    "list.updated",
    "list.deleted",
    "list-attribute.created",
    "list-attribute.updated",
    "list-entry.created",
    "list-entry.updated",
    "list-entry.deleted",
    "object-attribute.created",
    "object-attribute.updated",
    "note.created",
    "note-content.updated",
    "note.updated",
    "note.deleted",
    "record.created",
    "record.merged",
    "record.updated",
    "record.deleted",
    "task.created",
    "task.updated",
    "task.deleted",
    "workspace-member.created",
]
r"""Type of event the webhook is subscribed to."""


PatchV2WebhooksWebhookIDDollarAndOperatorNotEqualsRequest = Literal["not_equals",]


class PatchV2WebhooksWebhookIDDollarAndNotEqualsRequestTypedDict(TypedDict):
    field: str
    operator: PatchV2WebhooksWebhookIDDollarAndOperatorNotEqualsRequest
    value: str


class PatchV2WebhooksWebhookIDDollarAndNotEqualsRequest(BaseModel):
    field: str

    operator: PatchV2WebhooksWebhookIDDollarAndOperatorNotEqualsRequest

    value: str


PatchV2WebhooksWebhookIDDollarAndOperatorEqualsRequest = Literal["equals",]


class PatchV2WebhooksWebhookIDDollarAndEqualsRequestTypedDict(TypedDict):
    field: str
    operator: PatchV2WebhooksWebhookIDDollarAndOperatorEqualsRequest
    value: str


class PatchV2WebhooksWebhookIDDollarAndEqualsRequest(BaseModel):
    field: str

    operator: PatchV2WebhooksWebhookIDDollarAndOperatorEqualsRequest

    value: str


PatchV2WebhooksWebhookIDDollarAndRequestUnionTypedDict = TypeAliasType(
    "PatchV2WebhooksWebhookIDDollarAndRequestUnionTypedDict",
    Union[
        PatchV2WebhooksWebhookIDDollarAndEqualsRequestTypedDict,
        PatchV2WebhooksWebhookIDDollarAndNotEqualsRequestTypedDict,
    ],
)


PatchV2WebhooksWebhookIDDollarAndRequestUnion = TypeAliasType(
    "PatchV2WebhooksWebhookIDDollarAndRequestUnion",
    Union[
        PatchV2WebhooksWebhookIDDollarAndEqualsRequest,
        PatchV2WebhooksWebhookIDDollarAndNotEqualsRequest,
    ],
)


class PatchV2WebhooksWebhookIDFilterRequest2TypedDict(TypedDict):
    dollar_and: List[PatchV2WebhooksWebhookIDDollarAndRequestUnionTypedDict]


class PatchV2WebhooksWebhookIDFilterRequest2(BaseModel):
    dollar_and: Annotated[
        List[PatchV2WebhooksWebhookIDDollarAndRequestUnion],
        pydantic.Field(alias="$and"),
    ]


PatchV2WebhooksWebhookIDDollarOrOperatorNotEqualsRequest = Literal["not_equals",]


class PatchV2WebhooksWebhookIDDollarOrNotEqualsRequestTypedDict(TypedDict):
    field: str
    operator: PatchV2WebhooksWebhookIDDollarOrOperatorNotEqualsRequest
    value: str


class PatchV2WebhooksWebhookIDDollarOrNotEqualsRequest(BaseModel):
    field: str

    operator: PatchV2WebhooksWebhookIDDollarOrOperatorNotEqualsRequest

    value: str


PatchV2WebhooksWebhookIDDollarOrOperatorEqualsRequest = Literal["equals",]


class PatchV2WebhooksWebhookIDDollarOrEqualsRequestTypedDict(TypedDict):
    field: str
    operator: PatchV2WebhooksWebhookIDDollarOrOperatorEqualsRequest
    value: str


class PatchV2WebhooksWebhookIDDollarOrEqualsRequest(BaseModel):
    field: str

    operator: PatchV2WebhooksWebhookIDDollarOrOperatorEqualsRequest

    value: str


PatchV2WebhooksWebhookIDDollarOrRequestUnionTypedDict = TypeAliasType(
    "PatchV2WebhooksWebhookIDDollarOrRequestUnionTypedDict",
    Union[
        PatchV2WebhooksWebhookIDDollarOrEqualsRequestTypedDict,
        PatchV2WebhooksWebhookIDDollarOrNotEqualsRequestTypedDict,
    ],
)


PatchV2WebhooksWebhookIDDollarOrRequestUnion = TypeAliasType(
    "PatchV2WebhooksWebhookIDDollarOrRequestUnion",
    Union[
        PatchV2WebhooksWebhookIDDollarOrEqualsRequest,
        PatchV2WebhooksWebhookIDDollarOrNotEqualsRequest,
    ],
)


class PatchV2WebhooksWebhookIDFilterRequest1TypedDict(TypedDict):
    dollar_or: List[PatchV2WebhooksWebhookIDDollarOrRequestUnionTypedDict]


class PatchV2WebhooksWebhookIDFilterRequest1(BaseModel):
    dollar_or: Annotated[
        List[PatchV2WebhooksWebhookIDDollarOrRequestUnion], pydantic.Field(alias="$or")
    ]


PatchV2WebhooksWebhookIDFilterRequestUnionTypedDict = TypeAliasType(
    "PatchV2WebhooksWebhookIDFilterRequestUnionTypedDict",
    Union[
        PatchV2WebhooksWebhookIDFilterRequest1TypedDict,
        PatchV2WebhooksWebhookIDFilterRequest2TypedDict,
    ],
)
r"""Filters to determine whether the webhook event should be sent. If null, the filter always passes."""


PatchV2WebhooksWebhookIDFilterRequestUnion = TypeAliasType(
    "PatchV2WebhooksWebhookIDFilterRequestUnion",
    Union[
        PatchV2WebhooksWebhookIDFilterRequest1, PatchV2WebhooksWebhookIDFilterRequest2
    ],
)
r"""Filters to determine whether the webhook event should be sent. If null, the filter always passes."""


class PatchV2WebhooksWebhookIDSubscriptionRequestTypedDict(TypedDict):
    event_type: PatchV2WebhooksWebhookIDEventTypeRequest
    r"""Type of event the webhook is subscribed to."""
    filter_: Nullable[PatchV2WebhooksWebhookIDFilterRequestUnionTypedDict]
    r"""Filters to determine whether the webhook event should be sent. If null, the filter always passes."""


class PatchV2WebhooksWebhookIDSubscriptionRequest(BaseModel):
    event_type: PatchV2WebhooksWebhookIDEventTypeRequest
    r"""Type of event the webhook is subscribed to."""

    filter_: Annotated[
        Nullable[PatchV2WebhooksWebhookIDFilterRequestUnion],
        pydantic.Field(alias="filter"),
    ]
    r"""Filters to determine whether the webhook event should be sent. If null, the filter always passes."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = []
        nullable_fields = ["filter"]
        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 PatchV2WebhooksWebhookIDDataRequestTypedDict(TypedDict):
    target_url: NotRequired[str]
    r"""URL where the webhook events will be delivered to."""
    subscriptions: NotRequired[
        List[PatchV2WebhooksWebhookIDSubscriptionRequestTypedDict]
    ]
    r"""One or more events the webhook is subscribed to."""


class PatchV2WebhooksWebhookIDDataRequest(BaseModel):
    target_url: Optional[str] = None
    r"""URL where the webhook events will be delivered to."""

    subscriptions: Optional[List[PatchV2WebhooksWebhookIDSubscriptionRequest]] = None
    r"""One or more events the webhook is subscribed to."""


class PatchV2WebhooksWebhookIDRequestBodyTypedDict(TypedDict):
    data: PatchV2WebhooksWebhookIDDataRequestTypedDict


class PatchV2WebhooksWebhookIDRequestBody(BaseModel):
    data: PatchV2WebhooksWebhookIDDataRequest


class PatchV2WebhooksWebhookIDRequestTypedDict(TypedDict):
    webhook_id: str
    request_body: PatchV2WebhooksWebhookIDRequestBodyTypedDict


class PatchV2WebhooksWebhookIDRequest(BaseModel):
    webhook_id: Annotated[
        str, FieldMetadata(path=PathParamMetadata(style="simple", explode=False))
    ]

    request_body: Annotated[
        PatchV2WebhooksWebhookIDRequestBody,
        FieldMetadata(request=RequestMetadata(media_type="application/json")),
    ]


PatchV2WebhooksWebhookIDType = Literal["invalid_request_error",]


PatchV2WebhooksWebhookIDCode = Literal["not_found",]


PatchV2WebhooksWebhookIDEventTypeResponse = Literal[
    "call-recording.created",
    "comment.created",
    "comment.resolved",
    "comment.unresolved",
    "comment.deleted",
    "list.created",
    "list.updated",
    "list.deleted",
    "list-attribute.created",
    "list-attribute.updated",
    "list-entry.created",
    "list-entry.updated",
    "list-entry.deleted",
    "object-attribute.created",
    "object-attribute.updated",
    "note.created",
    "note-content.updated",
    "note.updated",
    "note.deleted",
    "record.created",
    "record.merged",
    "record.updated",
    "record.deleted",
    "task.created",
    "task.updated",
    "task.deleted",
    "workspace-member.created",
]
r"""Type of event the webhook is subscribed to."""


PatchV2WebhooksWebhookIDDollarAndOperatorNotEqualsResponse = Literal["not_equals",]


class PatchV2WebhooksWebhookIDDollarAndNotEqualsResponseTypedDict(TypedDict):
    field: str
    operator: PatchV2WebhooksWebhookIDDollarAndOperatorNotEqualsResponse
    value: str


class PatchV2WebhooksWebhookIDDollarAndNotEqualsResponse(BaseModel):
    field: str

    operator: PatchV2WebhooksWebhookIDDollarAndOperatorNotEqualsResponse

    value: str


PatchV2WebhooksWebhookIDDollarAndOperatorEqualsResponse = Literal["equals",]


class PatchV2WebhooksWebhookIDDollarAndEqualsResponseTypedDict(TypedDict):
    field: str
    operator: PatchV2WebhooksWebhookIDDollarAndOperatorEqualsResponse
    value: str


class PatchV2WebhooksWebhookIDDollarAndEqualsResponse(BaseModel):
    field: str

    operator: PatchV2WebhooksWebhookIDDollarAndOperatorEqualsResponse

    value: str


PatchV2WebhooksWebhookIDDollarAndResponseUnionTypedDict = TypeAliasType(
    "PatchV2WebhooksWebhookIDDollarAndResponseUnionTypedDict",
    Union[
        PatchV2WebhooksWebhookIDDollarAndEqualsResponseTypedDict,
        PatchV2WebhooksWebhookIDDollarAndNotEqualsResponseTypedDict,
    ],
)


PatchV2WebhooksWebhookIDDollarAndResponseUnion = TypeAliasType(
    "PatchV2WebhooksWebhookIDDollarAndResponseUnion",
    Union[
        PatchV2WebhooksWebhookIDDollarAndEqualsResponse,
        PatchV2WebhooksWebhookIDDollarAndNotEqualsResponse,
    ],
)


class PatchV2WebhooksWebhookIDFilterResponse2TypedDict(TypedDict):
    dollar_and: List[PatchV2WebhooksWebhookIDDollarAndResponseUnionTypedDict]


class PatchV2WebhooksWebhookIDFilterResponse2(BaseModel):
    dollar_and: Annotated[
        List[PatchV2WebhooksWebhookIDDollarAndResponseUnion],
        pydantic.Field(alias="$and"),
    ]


PatchV2WebhooksWebhookIDDollarOrOperatorNotEqualsResponse = Literal["not_equals",]


class PatchV2WebhooksWebhookIDDollarOrNotEqualsResponseTypedDict(TypedDict):
    field: str
    operator: PatchV2WebhooksWebhookIDDollarOrOperatorNotEqualsResponse
    value: str


class PatchV2WebhooksWebhookIDDollarOrNotEqualsResponse(BaseModel):
    field: str

    operator: PatchV2WebhooksWebhookIDDollarOrOperatorNotEqualsResponse

    value: str


PatchV2WebhooksWebhookIDDollarOrOperatorEqualsResponse = Literal["equals",]


class PatchV2WebhooksWebhookIDDollarOrEqualsResponseTypedDict(TypedDict):
    field: str
    operator: PatchV2WebhooksWebhookIDDollarOrOperatorEqualsResponse
    value: str


class PatchV2WebhooksWebhookIDDollarOrEqualsResponse(BaseModel):
    field: str

    operator: PatchV2WebhooksWebhookIDDollarOrOperatorEqualsResponse

    value: str


PatchV2WebhooksWebhookIDDollarOrResponseUnionTypedDict = TypeAliasType(
    "PatchV2WebhooksWebhookIDDollarOrResponseUnionTypedDict",
    Union[
        PatchV2WebhooksWebhookIDDollarOrEqualsResponseTypedDict,
        PatchV2WebhooksWebhookIDDollarOrNotEqualsResponseTypedDict,
    ],
)


PatchV2WebhooksWebhookIDDollarOrResponseUnion = TypeAliasType(
    "PatchV2WebhooksWebhookIDDollarOrResponseUnion",
    Union[
        PatchV2WebhooksWebhookIDDollarOrEqualsResponse,
        PatchV2WebhooksWebhookIDDollarOrNotEqualsResponse,
    ],
)


class PatchV2WebhooksWebhookIDFilterResponse1TypedDict(TypedDict):
    dollar_or: List[PatchV2WebhooksWebhookIDDollarOrResponseUnionTypedDict]


class PatchV2WebhooksWebhookIDFilterResponse1(BaseModel):
    dollar_or: Annotated[
        List[PatchV2WebhooksWebhookIDDollarOrResponseUnion], pydantic.Field(alias="$or")
    ]


PatchV2WebhooksWebhookIDFilterResponseUnionTypedDict = TypeAliasType(
    "PatchV2WebhooksWebhookIDFilterResponseUnionTypedDict",
    Union[
        PatchV2WebhooksWebhookIDFilterResponse1TypedDict,
        PatchV2WebhooksWebhookIDFilterResponse2TypedDict,
    ],
)
r"""Filters to determine whether the webhook event should be sent. If null, the filter always passes."""


PatchV2WebhooksWebhookIDFilterResponseUnion = TypeAliasType(
    "PatchV2WebhooksWebhookIDFilterResponseUnion",
    Union[
        PatchV2WebhooksWebhookIDFilterResponse1, PatchV2WebhooksWebhookIDFilterResponse2
    ],
)
r"""Filters to determine whether the webhook event should be sent. If null, the filter always passes."""


class PatchV2WebhooksWebhookIDSubscriptionResponseTypedDict(TypedDict):
    event_type: PatchV2WebhooksWebhookIDEventTypeResponse
    r"""Type of event the webhook is subscribed to."""
    filter_: Nullable[PatchV2WebhooksWebhookIDFilterResponseUnionTypedDict]
    r"""Filters to determine whether the webhook event should be sent. If null, the filter always passes."""


class PatchV2WebhooksWebhookIDSubscriptionResponse(BaseModel):
    event_type: PatchV2WebhooksWebhookIDEventTypeResponse
    r"""Type of event the webhook is subscribed to."""

    filter_: Annotated[
        Nullable[PatchV2WebhooksWebhookIDFilterResponseUnion],
        pydantic.Field(alias="filter"),
    ]
    r"""Filters to determine whether the webhook event should be sent. If null, the filter always passes."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = []
        nullable_fields = ["filter"]
        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 PatchV2WebhooksWebhookIDIDTypedDict(TypedDict):
    workspace_id: str
    r"""The ID of the workspace the webhook belongs to."""
    webhook_id: str
    r"""The ID of the webhook."""


class PatchV2WebhooksWebhookIDID(BaseModel):
    workspace_id: str
    r"""The ID of the workspace the webhook belongs to."""

    webhook_id: str
    r"""The ID of the webhook."""


PatchV2WebhooksWebhookIDStatus = Literal[
    "active",
    "degraded",
    "inactive",
]
r"""The state of the webhook. Webhooks marked as active and degraded will receive events, inactive ones will not. If a webhook remains in the degraded state for 7 days, it will be marked inactive."""


class PatchV2WebhooksWebhookIDDataResponseTypedDict(TypedDict):
    target_url: str
    r"""URL where the webhook events will be delivered to."""
    subscriptions: List[PatchV2WebhooksWebhookIDSubscriptionResponseTypedDict]
    r"""One or more events the webhook is subscribed to."""
    id: PatchV2WebhooksWebhookIDIDTypedDict
    status: PatchV2WebhooksWebhookIDStatus
    r"""The state of the webhook. Webhooks marked as active and degraded will receive events, inactive ones will not. If a webhook remains in the degraded state for 7 days, it will be marked inactive."""
    created_at: str
    r"""When the webhook was created."""


class PatchV2WebhooksWebhookIDDataResponse(BaseModel):
    target_url: str
    r"""URL where the webhook events will be delivered to."""

    subscriptions: List[PatchV2WebhooksWebhookIDSubscriptionResponse]
    r"""One or more events the webhook is subscribed to."""

    id: PatchV2WebhooksWebhookIDID

    status: PatchV2WebhooksWebhookIDStatus
    r"""The state of the webhook. Webhooks marked as active and degraded will receive events, inactive ones will not. If a webhook remains in the degraded state for 7 days, it will be marked inactive."""

    created_at: str
    r"""When the webhook was created."""


class PatchV2WebhooksWebhookIDResponseTypedDict(TypedDict):
    r"""Success"""

    data: PatchV2WebhooksWebhookIDDataResponseTypedDict


class PatchV2WebhooksWebhookIDResponse(BaseModel):
    r"""Success"""

    data: PatchV2WebhooksWebhookIDDataResponse
