"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
# @generated-id: d6adc687c2d9

from __future__ import annotations
from .judgeclassificationoutput import (
    JudgeClassificationOutput,
    JudgeClassificationOutputTypedDict,
)
from .judgeregressionoutput import JudgeRegressionOutput, JudgeRegressionOutputTypedDict
from datetime import datetime
from functools import partial
from mistralai.client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from mistralai.client.utils.unions import parse_open_union
from pydantic import ConfigDict, model_serializer
from pydantic.functional_validators import BeforeValidator
from typing import Any, List, Literal, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


JudgeOutputUnionTypedDict = TypeAliasType(
    "JudgeOutputUnionTypedDict",
    Union[JudgeClassificationOutputTypedDict, JudgeRegressionOutputTypedDict],
)


class UnknownJudgeOutputUnion(BaseModel):
    r"""A JudgeOutputUnion variant the SDK doesn't recognize. Preserves the raw payload."""

    type: Literal["UNKNOWN"] = "UNKNOWN"
    raw: Any
    is_unknown: Literal[True] = True

    model_config = ConfigDict(frozen=True)


_JUDGE_OUTPUT_UNION_VARIANTS: dict[str, Any] = {
    "CLASSIFICATION": JudgeClassificationOutput,
    "REGRESSION": JudgeRegressionOutput,
}


JudgeOutputUnion = Annotated[
    Union[JudgeClassificationOutput, JudgeRegressionOutput, UnknownJudgeOutputUnion],
    BeforeValidator(
        partial(
            parse_open_union,
            disc_key="type",
            variants=_JUDGE_OUTPUT_UNION_VARIANTS,
            unknown_cls=UnknownJudgeOutputUnion,
            union_name="JudgeOutputUnion",
        )
    ),
]


class JudgeTypedDict(TypedDict):
    id: str
    created_at: datetime
    updated_at: datetime
    deleted_at: Nullable[datetime]
    owner_id: str
    workspace_id: str
    name: str
    description: str
    model_name: str
    output: JudgeOutputUnionTypedDict
    instructions: str
    tools: List[str]
    up_revision: NotRequired[Nullable[str]]
    down_revision: NotRequired[Nullable[str]]
    base_revision: NotRequired[Nullable[str]]


class Judge(BaseModel):
    id: str

    created_at: datetime

    updated_at: datetime

    deleted_at: Nullable[datetime]

    owner_id: str

    workspace_id: str

    name: str

    description: str

    model_name: str

    output: JudgeOutputUnion

    instructions: str

    tools: List[str]

    up_revision: OptionalNullable[str] = UNSET

    down_revision: OptionalNullable[str] = UNSET

    base_revision: OptionalNullable[str] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["up_revision", "down_revision", "base_revision"])
        nullable_fields = set(
            ["deleted_at", "up_revision", "down_revision", "base_revision"]
        )
        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
