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.legacytypes_client_access import LegacytypesClientAccess
    from ..models.legacytypes_team import LegacytypesTeam
    from ..models.legacytypes_user import LegacytypesUser


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


@_attrs_define
class LegacytypesSession:
    """
    Attributes:
        field_id (str | Unset):
        active (bool | Unset):
        automation (bool | Unset): Used to track if the session is for an automation (e.g. API key) or a user
        client (LegacytypesClientAccess | Unset):
        created_on (str | Unset):
        exchanged (bool | Unset): If the session ID is exchanged for a token, we need to set it here so it's not
            exchanged again
        expires_on (str | Unset):
        ip (str | Unset):
        mechanism (str | Unset):
        otp (str | Unset): If 2FA is required, an OTP is generated and sent to the user
            As long as the OTP exists here, the session is NOT valid. When the user enters the OTP, it gets cleared from the
            session and the session becomes valid.
        refresh_on (str | Unset): The session needs to automatically refresh itself every 5 minutes to keep the data
            from going stale.
        team (LegacytypesTeam | Unset):
        token (str | Unset):
        type_ (str | Unset): Session type: Normal | Delegate | Desktop | Admin
        updated_on (str | Unset): Used to track if the session data is stale. The client passes a `x-session-update`
            header which must be before or at the update time.
            If not, the session is refreshed
        user (LegacytypesUser | Unset):
        user_agent (str | Unset):
    """

    field_id: str | Unset = UNSET
    active: bool | Unset = UNSET
    automation: bool | Unset = UNSET
    client: LegacytypesClientAccess | Unset = UNSET
    created_on: str | Unset = UNSET
    exchanged: bool | Unset = UNSET
    expires_on: str | Unset = UNSET
    ip: str | Unset = UNSET
    mechanism: str | Unset = UNSET
    otp: str | Unset = UNSET
    refresh_on: str | Unset = UNSET
    team: LegacytypesTeam | Unset = UNSET
    token: str | Unset = UNSET
    type_: str | Unset = UNSET
    updated_on: str | Unset = UNSET
    user: LegacytypesUser | Unset = UNSET
    user_agent: 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

        active = self.active

        automation = self.automation

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

        created_on = self.created_on

        exchanged = self.exchanged

        expires_on = self.expires_on

        ip = self.ip

        mechanism = self.mechanism

        otp = self.otp

        refresh_on = self.refresh_on

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

        token = self.token

        type_ = self.type_

        updated_on = self.updated_on

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

        user_agent = self.user_agent

        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 active is not UNSET:
            field_dict["active"] = active
        if automation is not UNSET:
            field_dict["automation"] = automation
        if client is not UNSET:
            field_dict["client"] = client
        if created_on is not UNSET:
            field_dict["created_on"] = created_on
        if exchanged is not UNSET:
            field_dict["exchanged"] = exchanged
        if expires_on is not UNSET:
            field_dict["expires_on"] = expires_on
        if ip is not UNSET:
            field_dict["ip"] = ip
        if mechanism is not UNSET:
            field_dict["mechanism"] = mechanism
        if otp is not UNSET:
            field_dict["otp"] = otp
        if refresh_on is not UNSET:
            field_dict["refreshOn"] = refresh_on
        if team is not UNSET:
            field_dict["team"] = team
        if token is not UNSET:
            field_dict["token"] = token
        if type_ is not UNSET:
            field_dict["type"] = type_
        if updated_on is not UNSET:
            field_dict["updatedOn"] = updated_on
        if user is not UNSET:
            field_dict["user"] = user
        if user_agent is not UNSET:
            field_dict["user_agent"] = user_agent

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.legacytypes_client_access import LegacytypesClientAccess
        from ..models.legacytypes_team import LegacytypesTeam
        from ..models.legacytypes_user import LegacytypesUser

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

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

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

        _client = d.pop("client", UNSET)
        client: LegacytypesClientAccess | Unset
        if isinstance(_client, Unset):
            client = UNSET
        else:
            client = LegacytypesClientAccess.from_dict(_client)

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

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

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

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

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

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

        refresh_on = d.pop("refreshOn", UNSET)

        _team = d.pop("team", UNSET)
        team: LegacytypesTeam | Unset
        if isinstance(_team, Unset):
            team = UNSET
        else:
            team = LegacytypesTeam.from_dict(_team)

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

        type_ = d.pop("type", UNSET)

        updated_on = d.pop("updatedOn", UNSET)

        _user = d.pop("user", UNSET)
        user: LegacytypesUser | Unset
        if isinstance(_user, Unset):
            user = UNSET
        else:
            user = LegacytypesUser.from_dict(_user)

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

        legacytypes_session = cls(
            field_id=field_id,
            active=active,
            automation=automation,
            client=client,
            created_on=created_on,
            exchanged=exchanged,
            expires_on=expires_on,
            ip=ip,
            mechanism=mechanism,
            otp=otp,
            refresh_on=refresh_on,
            team=team,
            token=token,
            type_=type_,
            updated_on=updated_on,
            user=user,
            user_agent=user_agent,
        )

        legacytypes_session.additional_properties = d
        return legacytypes_session

    @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
