from __future__ import annotations

import datetime
from collections.abc import Mapping
from typing import Any, TypeVar, cast
from uuid import UUID

from attrs import define as _attrs_define
from attrs import field as _attrs_field
from dateutil.parser import isoparse

from ..types import UNSET, Unset

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


@_attrs_define
class PasskeyRecord:
    """
    Attributes:
        id (UUID):
        friendly_name (None | str):
        purposes (list[str]): Passkey purposes (login, delegation_approval)
        created_at (datetime.datetime):
        last_used_at (datetime.datetime | None | Unset):
    """

    id: UUID
    friendly_name: None | str
    purposes: list[str]
    created_at: datetime.datetime
    last_used_at: datetime.datetime | None | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        friendly_name: None | str
        friendly_name = self.friendly_name

        purposes = self.purposes

        created_at = self.created_at.isoformat()

        last_used_at: None | str | Unset
        if isinstance(self.last_used_at, Unset):
            last_used_at = UNSET
        elif isinstance(self.last_used_at, datetime.datetime):
            last_used_at = self.last_used_at.isoformat()
        else:
            last_used_at = self.last_used_at

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "id": id,
                "friendly_name": friendly_name,
                "purposes": purposes,
                "created_at": created_at,
            }
        )
        if last_used_at is not UNSET:
            field_dict["last_used_at"] = last_used_at

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        d = dict(src_dict)
        id = UUID(d.pop("id"))

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

        friendly_name = _parse_friendly_name(d.pop("friendly_name"))

        purposes = cast(list[str], d.pop("purposes"))

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

        def _parse_last_used_at(data: object) -> datetime.datetime | None | Unset:
            if data is None:
                return data
            if isinstance(data, Unset):
                return data
            try:
                if not isinstance(data, str):
                    raise TypeError()
                last_used_at_type_0 = isoparse(data)

                return last_used_at_type_0
            except (TypeError, ValueError, AttributeError, KeyError):
                pass
            return cast(datetime.datetime | None | Unset, data)

        last_used_at = _parse_last_used_at(d.pop("last_used_at", UNSET))

        passkey_record = cls(
            id=id,
            friendly_name=friendly_name,
            purposes=purposes,
            created_at=created_at,
            last_used_at=last_used_at,
        )

        passkey_record.additional_properties = d
        return passkey_record

    @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
