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.form_section import FormSection


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


@_attrs_define
class Form:
    """
    Attributes:
        field_id (str | Unset): MongoDB ObjectId
        team (str | Unset): MongoDB ObjectId
        title (str | Unset):
        description (str | Unset):
        created_on (datetime.datetime | Unset):
        last_modified (datetime.datetime | Unset):
        sections (list[FormSection] | Unset):
    """

    field_id: str | Unset = UNSET
    team: str | Unset = UNSET
    title: str | Unset = UNSET
    description: str | Unset = UNSET
    created_on: datetime.datetime | Unset = UNSET
    last_modified: datetime.datetime | Unset = UNSET
    sections: list[FormSection] | 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

        team = self.team

        title = self.title

        description = self.description

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

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

        sections: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.sections, Unset):
            sections = []
            for sections_item_data in self.sections:
                sections_item = sections_item_data.to_dict()
                sections.append(sections_item)

        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 team is not UNSET:
            field_dict["team"] = team
        if title is not UNSET:
            field_dict["title"] = title
        if description is not UNSET:
            field_dict["description"] = description
        if created_on is not UNSET:
            field_dict["createdOn"] = created_on
        if last_modified is not UNSET:
            field_dict["lastModified"] = last_modified
        if sections is not UNSET:
            field_dict["sections"] = sections

        return field_dict

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

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

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

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

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

        _created_on = d.pop("createdOn", UNSET)
        created_on: datetime.datetime | Unset
        if isinstance(_created_on, Unset):
            created_on = UNSET
        else:
            created_on = isoparse(_created_on)

        _last_modified = d.pop("lastModified", UNSET)
        last_modified: datetime.datetime | Unset
        if isinstance(_last_modified, Unset):
            last_modified = UNSET
        else:
            last_modified = isoparse(_last_modified)

        _sections = d.pop("sections", UNSET)
        sections: list[FormSection] | Unset = UNSET
        if _sections is not UNSET:
            sections = []
            for sections_item_data in _sections:
                sections_item = FormSection.from_dict(sections_item_data)

                sections.append(sections_item)

        form = cls(
            field_id=field_id,
            team=team,
            title=title,
            description=description,
            created_on=created_on,
            last_modified=last_modified,
            sections=sections,
        )

        form.additional_properties = d
        return form

    @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
