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

from __future__ import annotations
from .alternativequery import AlternativeQuery, AlternativeQueryTypedDict
from .convertqueryerror import ConvertQueryError, ConvertQueryErrorTypedDict
from censys_platform.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from enum import Enum
from pydantic import model_serializer
from typing import List, Optional
from typing_extensions import NotRequired, TypedDict


class SearchConvertQueryResponseTargets(str, Enum):
    HOST = "host"
    WEB = "web"
    CERT = "cert"
    UNKNOWN = "unknown"


class SearchConvertQueryResponseTypedDict(TypedDict):
    errors: Nullable[List[ConvertQueryErrorTypedDict]]
    r"""The error messages if the query could not be converted."""
    original_query: str
    r"""The original Censys Search Language query."""
    targets: Nullable[List[SearchConvertQueryResponseTargets]]
    r"""The types of Platform data records (host, cert, and web) that can be targeted by the converted query. Note: Does not include targets that could be returned due to full-document search. This may be empty if the query is a match-all for full-document search query."""
    alternative_queries: NotRequired[Nullable[List[AlternativeQueryTypedDict]]]
    r"""Alternative queries that may be used. For example, a query that includes web results in addition to host results."""
    comments: NotRequired[Nullable[List[str]]]
    r"""Comments about the query conversion."""
    converted_query: NotRequired[str]
    r"""The original query, converted to CenQL syntax. This may be empty if unable to translate the query."""


class SearchConvertQueryResponse(BaseModel):
    errors: Nullable[List[ConvertQueryError]]
    r"""The error messages if the query could not be converted."""

    original_query: str
    r"""The original Censys Search Language query."""

    targets: Nullable[List[SearchConvertQueryResponseTargets]]
    r"""The types of Platform data records (host, cert, and web) that can be targeted by the converted query. Note: Does not include targets that could be returned due to full-document search. This may be empty if the query is a match-all for full-document search query."""

    alternative_queries: OptionalNullable[List[AlternativeQuery]] = UNSET
    r"""Alternative queries that may be used. For example, a query that includes web results in addition to host results."""

    comments: OptionalNullable[List[str]] = UNSET
    r"""Comments about the query conversion."""

    converted_query: Optional[str] = None
    r"""The original query, converted to CenQL syntax. This may be empty if unable to translate the query."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["alternative_queries", "comments", "converted_query"]
        nullable_fields = ["alternative_queries", "comments", "errors", "targets"]
        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
