from __future__ import annotations

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

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.ask_config import AskConfig
    from ..models.membership import Membership
    from ..models.notification import Notification
    from ..models.user_state import UserState
    from ..models.user_tips import UserTips


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


@_attrs_define
class User:
    """
    Attributes:
        field_id (str | Unset): MongoDB ObjectId
        name (str | Unset):
        email (str | Unset):
        memberships (list[Membership] | Unset):
        team (str | Unset): MongoDB ObjectId
        is_pending (bool | Unset):
        last_access (datetime.datetime | Unset):
        enable_dark_mode (bool | Unset):
        signature (str | Unset):
        welcomed (bool | Unset):
        splash (str | Unset):
        tips (UserTips | Unset):
        ask (AskConfig | Unset):
        notifications (list[Notification] | Unset):
        last_local_sync (datetime.datetime | Unset):
        state (UserState | Unset):
        intercom_id (str | Unset):
    """

    field_id: str | Unset = UNSET
    name: str | Unset = UNSET
    email: str | Unset = UNSET
    memberships: list[Membership] | Unset = UNSET
    team: str | Unset = UNSET
    is_pending: bool | Unset = UNSET
    last_access: datetime.datetime | Unset = UNSET
    enable_dark_mode: bool | Unset = UNSET
    signature: str | Unset = UNSET
    welcomed: bool | Unset = UNSET
    splash: str | Unset = UNSET
    tips: UserTips | Unset = UNSET
    ask: AskConfig | Unset = UNSET
    notifications: list[Notification] | Unset = UNSET
    last_local_sync: datetime.datetime | Unset = UNSET
    state: UserState | Unset = UNSET
    intercom_id: str | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        name = self.name

        email = self.email

        memberships: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.memberships, Unset):
            memberships = []
            for memberships_item_data in self.memberships:
                memberships_item = memberships_item_data.to_dict()
                memberships.append(memberships_item)

        team = self.team

        is_pending = self.is_pending

        last_access: str | Unset = UNSET
        if not isinstance(self.last_access, Unset):
            last_access = self.last_access.isoformat()

        enable_dark_mode = self.enable_dark_mode

        signature = self.signature

        welcomed = self.welcomed

        splash = self.splash

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

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

        notifications: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.notifications, Unset):
            notifications = []
            for notifications_item_data in self.notifications:
                notifications_item = notifications_item_data.to_dict()
                notifications.append(notifications_item)

        last_local_sync: str | Unset = UNSET
        if not isinstance(self.last_local_sync, Unset):
            last_local_sync = self.last_local_sync.isoformat()

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

        intercom_id = self.intercom_id

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if field_id is not UNSET:
            field_dict["_id"] = field_id
        if name is not UNSET:
            field_dict["name"] = name
        if email is not UNSET:
            field_dict["email"] = email
        if memberships is not UNSET:
            field_dict["memberships"] = memberships
        if team is not UNSET:
            field_dict["team"] = team
        if is_pending is not UNSET:
            field_dict["isPending"] = is_pending
        if last_access is not UNSET:
            field_dict["lastAccess"] = last_access
        if enable_dark_mode is not UNSET:
            field_dict["enableDarkMode"] = enable_dark_mode
        if signature is not UNSET:
            field_dict["signature"] = signature
        if welcomed is not UNSET:
            field_dict["welcomed"] = welcomed
        if splash is not UNSET:
            field_dict["splash"] = splash
        if tips is not UNSET:
            field_dict["tips"] = tips
        if ask is not UNSET:
            field_dict["ask"] = ask
        if notifications is not UNSET:
            field_dict["notifications"] = notifications
        if last_local_sync is not UNSET:
            field_dict["lastLocalSync"] = last_local_sync
        if state is not UNSET:
            field_dict["state"] = state
        if intercom_id is not UNSET:
            field_dict["intercomId"] = intercom_id

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.ask_config import AskConfig
        from ..models.membership import Membership
        from ..models.notification import Notification
        from ..models.user_state import UserState
        from ..models.user_tips import UserTips

        d = dict(src_dict)
        field_id = d.pop("_id", UNSET)

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

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

        _memberships = d.pop("memberships", UNSET)
        memberships: list[Membership] | Unset = UNSET
        if _memberships is not UNSET:
            memberships = []
            for memberships_item_data in _memberships:
                memberships_item = Membership.from_dict(memberships_item_data)

                memberships.append(memberships_item)

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

        is_pending = d.pop("isPending", UNSET)

        _last_access = d.pop("lastAccess", UNSET)
        last_access: datetime.datetime | Unset
        if isinstance(_last_access, Unset):
            last_access = UNSET
        else:
            last_access = isoparse(_last_access)

        enable_dark_mode = d.pop("enableDarkMode", UNSET)

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

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

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

        _tips = d.pop("tips", UNSET)
        tips: UserTips | Unset
        if isinstance(_tips, Unset):
            tips = UNSET
        else:
            tips = UserTips.from_dict(_tips)

        _ask = d.pop("ask", UNSET)
        ask: AskConfig | Unset
        if isinstance(_ask, Unset):
            ask = UNSET
        else:
            ask = AskConfig.from_dict(_ask)

        _notifications = d.pop("notifications", UNSET)
        notifications: list[Notification] | Unset = UNSET
        if _notifications is not UNSET:
            notifications = []
            for notifications_item_data in _notifications:
                notifications_item = Notification.from_dict(notifications_item_data)

                notifications.append(notifications_item)

        _last_local_sync = d.pop("lastLocalSync", UNSET)
        last_local_sync: datetime.datetime | Unset
        if isinstance(_last_local_sync, Unset):
            last_local_sync = UNSET
        else:
            last_local_sync = isoparse(_last_local_sync)

        _state = d.pop("state", UNSET)
        state: UserState | Unset
        if isinstance(_state, Unset):
            state = UNSET
        else:
            state = UserState.from_dict(_state)

        intercom_id = d.pop("intercomId", UNSET)

        user = cls(
            field_id=field_id,
            name=name,
            email=email,
            memberships=memberships,
            team=team,
            is_pending=is_pending,
            last_access=last_access,
            enable_dark_mode=enable_dark_mode,
            signature=signature,
            welcomed=welcomed,
            splash=splash,
            tips=tips,
            ask=ask,
            notifications=notifications,
            last_local_sync=last_local_sync,
            state=state,
            intercom_id=intercom_id,
        )

        user.additional_properties = d
        return user

    @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
