from __future__ import annotations

import datetime
import json
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 dateutil.parser import isoparse

from .. import types
from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.group import Group
    from ..models.user import User


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


@_attrs_define
class UserSpace:
    """Adds nested create feature

    Attributes:
        user (User): Adds nested create feature
        space (int):
        groups (list[Group]):
        invite_link (int | None):
        created_at (datetime.datetime):
        updated_at (datetime.datetime):
        id (int | Unset):
        active (bool | Unset):
        internal_note (None | str | Unset):
    """

    user: User
    space: int
    groups: list[Group]
    invite_link: int | None
    created_at: datetime.datetime
    updated_at: datetime.datetime
    id: int | Unset = UNSET
    active: bool | Unset = UNSET
    internal_note: None | str | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        space = self.space

        groups = []
        for groups_item_data in self.groups:
            groups_item = groups_item_data.to_dict()
            groups.append(groups_item)

        invite_link: int | None
        invite_link = self.invite_link

        created_at = self.created_at.isoformat()

        updated_at = self.updated_at.isoformat()

        id = self.id

        active = self.active

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

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "user": user,
                "space": space,
                "groups": groups,
                "invite_link": invite_link,
                "created_at": created_at,
                "updated_at": updated_at,
            }
        )
        if id is not UNSET:
            field_dict["id"] = id
        if active is not UNSET:
            field_dict["active"] = active
        if internal_note is not UNSET:
            field_dict["internal_note"] = internal_note

        return field_dict

    def to_multipart(self) -> types.RequestFiles:
        files: types.RequestFiles = []

        files.append(("user", (None, json.dumps(self.user.to_dict()).encode(), "application/json")))

        files.append(("space", (None, str(self.space).encode(), "text/plain")))

        for groups_item_element in self.groups:
            files.append(("groups", (None, json.dumps(groups_item_element.to_dict()).encode(), "application/json")))

        if isinstance(self.invite_link, int):
            files.append(("invite_link", (None, str(self.invite_link).encode(), "text/plain")))
        else:
            files.append(("invite_link", (None, str(self.invite_link).encode(), "text/plain")))

        files.append(("created_at", (None, self.created_at.isoformat().encode(), "text/plain")))

        files.append(("updated_at", (None, self.updated_at.isoformat().encode(), "text/plain")))

        if not isinstance(self.id, Unset):
            files.append(("id", (None, str(self.id).encode(), "text/plain")))

        if not isinstance(self.active, Unset):
            files.append(("active", (None, str(self.active).encode(), "text/plain")))

        if not isinstance(self.internal_note, Unset):
            if isinstance(self.internal_note, str):
                files.append(("internal_note", (None, str(self.internal_note).encode(), "text/plain")))
            else:
                files.append(("internal_note", (None, str(self.internal_note).encode(), "text/plain")))

        for prop_name, prop in self.additional_properties.items():
            files.append((prop_name, (None, str(prop).encode(), "text/plain")))

        return files

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.group import Group
        from ..models.user import User

        d = dict(src_dict)
        user = User.from_dict(d.pop("user"))

        space = d.pop("space")

        groups = []
        _groups = d.pop("groups")
        for groups_item_data in _groups:
            groups_item = Group.from_dict(groups_item_data)

            groups.append(groups_item)

        def _parse_invite_link(data: object) -> int | None:
            if data is None:
                return data
            return cast(int | None, data)

        invite_link = _parse_invite_link(d.pop("invite_link"))

        created_at = isoparse(d.pop("created_at"))

        updated_at = isoparse(d.pop("updated_at"))

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

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

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

        internal_note = _parse_internal_note(d.pop("internal_note", UNSET))

        user_space = cls(
            user=user,
            space=space,
            groups=groups,
            invite_link=invite_link,
            created_at=created_at,
            updated_at=updated_at,
            id=id,
            active=active,
            internal_note=internal_note,
        )

        user_space.additional_properties = d
        return user_space

    @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
