from __future__ import annotations

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 ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.user import User


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


@_attrs_define
class Session:
    """
    Attributes:
        access_token (str):
        refresh_token (str):
        expires_in (int):
        token_type (str):
        user (User):
        provider_token (None | str | Unset):
        provider_refresh_token (None | str | Unset):
        expires_at (int | None | Unset):
    """

    access_token: str
    refresh_token: str
    expires_in: int
    token_type: str
    user: User
    provider_token: None | str | Unset = UNSET
    provider_refresh_token: None | str | Unset = UNSET
    expires_at: int | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        refresh_token = self.refresh_token

        expires_in = self.expires_in

        token_type = self.token_type

        user = self.user.to_dict()

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

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

        expires_at: int | None | Unset
        if isinstance(self.expires_at, Unset):
            expires_at = UNSET
        else:
            expires_at = self.expires_at

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "access_token": access_token,
                "refresh_token": refresh_token,
                "expires_in": expires_in,
                "token_type": token_type,
                "user": user,
            }
        )
        if provider_token is not UNSET:
            field_dict["provider_token"] = provider_token
        if provider_refresh_token is not UNSET:
            field_dict["provider_refresh_token"] = provider_refresh_token
        if expires_at is not UNSET:
            field_dict["expires_at"] = expires_at

        return field_dict

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

        d = dict(src_dict)
        access_token = d.pop("access_token")

        refresh_token = d.pop("refresh_token")

        expires_in = d.pop("expires_in")

        token_type = d.pop("token_type")

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

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

        provider_token = _parse_provider_token(d.pop("provider_token", UNSET))

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

        provider_refresh_token = _parse_provider_refresh_token(d.pop("provider_refresh_token", UNSET))

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

        expires_at = _parse_expires_at(d.pop("expires_at", UNSET))

        session = cls(
            access_token=access_token,
            refresh_token=refresh_token,
            expires_in=expires_in,
            token_type=token_type,
            user=user,
            provider_token=provider_token,
            provider_refresh_token=provider_refresh_token,
            expires_at=expires_at,
        )

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