from __future__ import annotations

from collections.abc import Mapping
from typing import Any, TypeVar, BinaryIO, TextIO, TYPE_CHECKING, Generator

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

from ..types import UNSET, Unset

from ..models.validation_error_code import ValidationErrorCode
from typing import cast






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



@_attrs_define
class ApiErrorItem:
    """ Детальная информация об ошибке

        Attributes:
            key (str): Ключ поля с ошибкой Example: field.name.
            value (None | str): Значение поля, которое вызвало ошибку Example: invalid_value.
            message (str): Сообщение об ошибке Example: Поле не может быть пустым.
            code (ValidationErrorCode): Коды ошибок валидации
            payload (None | str): Дополнительные данные об ошибке
     """

    key: str
    value: None | str
    message: str
    code: ValidationErrorCode
    payload: None | str
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)





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

        value: None | str
        value = self.value

        message = self.message

        code = self.code.value

        payload: None | str
        payload = self.payload


        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({
            "key": key,
            "value": value,
            "message": message,
            "code": code,
            "payload": payload,
        })

        return field_dict



    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        d = dict(src_dict)
        key = d.pop("key")

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

        value = _parse_value(d.pop("value"))


        message = d.pop("message")

        code = ValidationErrorCode(d.pop("code"))




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

        payload = _parse_payload(d.pop("payload"))


        api_error_item = cls(
            key=key,
            value=value,
            message=message,
            code=code,
            payload=payload,
        )


        api_error_item.additional_properties = d
        return api_error_item

    @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
