from __future__ import annotations

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

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

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.azure_blob_connection_settings_api_dto import AzureBlobConnectionSettingsApiDto
    from ..models.s3_connection_settings_api_dto import S3ConnectionSettingsApiDto


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


@_attrs_define
class CreateConnectionRequest:
    """
    Attributes:
        name (str | Unset):
        description (str | Unset):
        settings (AzureBlobConnectionSettingsApiDto | S3ConnectionSettingsApiDto | Unset):
    """

    name: str | Unset = UNSET
    description: str | Unset = UNSET
    settings: AzureBlobConnectionSettingsApiDto | S3ConnectionSettingsApiDto | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

    def to_dict(self) -> dict[str, Any]:
        from ..models.s3_connection_settings_api_dto import S3ConnectionSettingsApiDto

        name = self.name

        description = self.description

        settings: dict[str, Any] | Unset
        if isinstance(self.settings, Unset):
            settings = UNSET
        elif isinstance(self.settings, S3ConnectionSettingsApiDto):
            settings = self.settings.to_dict()
        else:
            settings = self.settings.to_dict()

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

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.azure_blob_connection_settings_api_dto import AzureBlobConnectionSettingsApiDto
        from ..models.s3_connection_settings_api_dto import S3ConnectionSettingsApiDto

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

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

        def _parse_settings(data: object) -> AzureBlobConnectionSettingsApiDto | S3ConnectionSettingsApiDto | Unset:
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, dict):
                    raise TypeError()
                componentsschemas_connection_settings_api_dto_type_0 = S3ConnectionSettingsApiDto.from_dict(data)

                return componentsschemas_connection_settings_api_dto_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            if not isinstance(data, dict):
                raise TypeError()
            componentsschemas_connection_settings_api_dto_type_1 = AzureBlobConnectionSettingsApiDto.from_dict(data)

            return componentsschemas_connection_settings_api_dto_type_1

        settings = _parse_settings(d.pop("settings", UNSET))

        create_connection_request = cls(
            name=name,
            description=description,
            settings=settings,
        )

        create_connection_request.additional_properties = d
        return create_connection_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
