from __future__ import annotations

from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, TypeVar, cast

from attrs import define as _attrs_define
from attrs import field as _attrs_field

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.structured_output_schema_properties import StructuredOutputSchemaProperties


T = TypeVar("T", bound="StructuredOutputSchema")


@_attrs_define
class StructuredOutputSchema:
    """Schema for structured outputs using JSON Schema.

    Attributes:
        properties (StructuredOutputSchemaProperties):
        type_ (str | Unset):  Default: 'object'.
        required (list[str] | None | Unset):
        description (None | str | Unset):
        strict (bool | Unset):  Default: True.
    """

    properties: StructuredOutputSchemaProperties
    type_: str | Unset = "object"
    required: list[str] | None | Unset = UNSET
    description: None | str | Unset = UNSET
    strict: bool | Unset = True
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        properties = self.properties.to_dict()

        type_ = self.type_

        required: list[str] | None | Unset
        if isinstance(self.required, Unset):
            required = UNSET
        elif isinstance(self.required, list):
            required = self.required

        else:
            required = self.required

        description: None | str | Unset
        if isinstance(self.description, Unset):
            description = UNSET
        else:
            description = self.description

        strict = self.strict

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "properties": properties,
            }
        )
        if type_ is not UNSET:
            field_dict["type"] = type_
        if required is not UNSET:
            field_dict["required"] = required
        if description is not UNSET:
            field_dict["description"] = description
        if strict is not UNSET:
            field_dict["strict"] = strict

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.structured_output_schema_properties import StructuredOutputSchemaProperties

        d = dict(src_dict)
        properties = StructuredOutputSchemaProperties.from_dict(d.pop("properties"))

        type_ = d.pop("type", UNSET)

        def _parse_required(data: object) -> list[str] | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, list):
                    raise TypeError()
                required_type_0 = cast(list[str], data)

                return required_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(list[str] | None | Unset, data)

        required = _parse_required(d.pop("required", UNSET))

        def _parse_description(data: object) -> None | str | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            return cast(None | str | Unset, data)

        description = _parse_description(d.pop("description", UNSET))

        strict = d.pop("strict", UNSET)

        structured_output_schema = cls(
            properties=properties,
            type_=type_,
            required=required,
            description=description,
            strict=strict,
        )

        structured_output_schema.additional_properties = d
        return structured_output_schema

    @property
    def additional_keys(self) -> list[str]:
        return list(self.additional_properties.keys())

    def __getitem__(self, key: str) -> Any:
        return self.additional_properties[key]

    def __setitem__(self, key: str, value: Any) -> None:
        self.additional_properties[key] = value

    def __delitem__(self, key: str) -> None:
        del self.additional_properties[key]

    def __contains__(self, key: str) -> bool:
        return key in self.additional_properties
