from __future__ import annotations

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

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

from ..types import UNSET, Unset

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


@_attrs_define
class PatchObjectRequest:
    """Request model for partially updating a bucket object (PATCH operation).

    Task 10: Use extra='allow' to accept any user-defined fields at root level.
    No nested metadata dict - all fields are flat.

        Attributes:
            key_prefix (None | str | Unset): Updated storage key/path prefix of the object
            skip_duplicates (bool | None | Unset): Skip duplicate blobs
    """

    key_prefix: None | str | Unset = UNSET
    skip_duplicates: bool | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        key_prefix: None | str | Unset
        if isinstance(self.key_prefix, Unset):
            key_prefix = UNSET
        else:
            key_prefix = self.key_prefix

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

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if key_prefix is not UNSET:
            field_dict["key_prefix"] = key_prefix
        if skip_duplicates is not UNSET:
            field_dict["skip_duplicates"] = skip_duplicates

        return field_dict

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

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

        key_prefix = _parse_key_prefix(d.pop("key_prefix", UNSET))

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

        skip_duplicates = _parse_skip_duplicates(d.pop("skip_duplicates", UNSET))

        patch_object_request = cls(
            key_prefix=key_prefix,
            skip_duplicates=skip_duplicates,
        )

        patch_object_request.additional_properties = d
        return patch_object_request

    @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
