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.json_schema_schema import JSONSchemaSchema


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


@_attrs_define
class JSONSchema:
    """Structured Outputs configuration options, including a JSON Schema.

    Attributes:
        name (str):
        description (str | Unset):
        schema (JSONSchemaSchema | Unset):
        strict (bool | None | Unset):
    """

    name: str
    description: str | Unset = UNSET
    schema: JSONSchemaSchema | Unset = UNSET
    strict: bool | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        description = self.description

        schema: dict[str, Any] | Unset = UNSET
        if not isinstance(self.schema, Unset):
            schema = self.schema.to_dict()

        strict: bool | None | Unset
        if isinstance(self.strict, Unset):
            strict = UNSET
        else:
            strict = self.strict

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "name": name,
            }
        )
        if description is not UNSET:
            field_dict["description"] = description
        if schema is not UNSET:
            field_dict["schema"] = schema
        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.json_schema_schema import JSONSchemaSchema

        d = dict(src_dict)
        name = d.pop("name")

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

        _schema = d.pop("schema", UNSET)
        schema: JSONSchemaSchema | Unset
        if isinstance(_schema, Unset):
            schema = UNSET
        else:
            schema = JSONSchemaSchema.from_dict(_schema)

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

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

        json_schema = cls(
            name=name,
            description=description,
            schema=schema,
            strict=strict,
        )

        json_schema.additional_properties = d
        return json_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
