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

from __future__ import annotations
from .issuestate import IssueState
from enum import Enum
from owasp_nest.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from owasp_nest.utils import FieldMetadata, QueryParamMetadata
from pydantic import model_serializer
from typing import Optional
from typing_extensions import Annotated, NotRequired, TypedDict


class ListMilestonesOrdering(str, Enum):
    CREATED_AT = "created_at"
    MINUS_CREATED_AT = "-created_at"
    UPDATED_AT = "updated_at"
    MINUS_UPDATED_AT = "-updated_at"


class ListMilestonesRequestTypedDict(TypedDict):
    organization: NotRequired[Nullable[str]]
    r"""Organization that milestones belong to (filtered by repository owner)"""
    repository: NotRequired[Nullable[str]]
    r"""Repository that milestones belong to"""
    state: NotRequired[Nullable[IssueState]]
    r"""Milestone state"""
    ordering: NotRequired[Nullable[ListMilestonesOrdering]]
    page: NotRequired[int]
    r"""Page number"""
    page_size: NotRequired[int]
    r"""Number of items per page"""


class ListMilestonesRequest(BaseModel):
    organization: Annotated[
        OptionalNullable[str],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Organization that milestones belong to (filtered by repository owner)"""

    repository: Annotated[
        OptionalNullable[str],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Repository that milestones belong to"""

    state: Annotated[
        OptionalNullable[IssueState],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET
    r"""Milestone state"""

    ordering: Annotated[
        OptionalNullable[ListMilestonesOrdering],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = UNSET

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

    page_size: Annotated[
        Optional[int],
        FieldMetadata(query=QueryParamMetadata(style="form", explode=True)),
    ] = 100
    r"""Number of items per page"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            ["organization", "repository", "state", "ordering", "page", "page_size"]
        )
        nullable_fields = set(["organization", "repository", "state", "ordering"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

            if val != UNSET_SENTINEL:
                if (
                    val is not None
                    or k not in optional_fields
                    or is_nullable_and_explicitly_set
                ):
                    m[k] = val

        return m
