from __future__ import annotations

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

from ..types import UNSET, Unset

if TYPE_CHECKING:
    from ..models.table_row import TableRow
    from ..models.table_tag import TableTag


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


@_attrs_define
class Table:
    """
    Attributes:
        raw (bool | Unset):
        title (str | Unset):
        last_modified (datetime.datetime | Unset):
        created_on (datetime.datetime | Unset):
        page (float | Unset):
        x (float | Unset):
        y (float | Unset):
        width (float | Unset):
        height (float | Unset):
        headers (list[str] | Unset):
        tags (list[TableTag] | Unset):
        rows (list[TableRow] | Unset):
    """

    raw: bool | Unset = UNSET
    title: str | Unset = UNSET
    last_modified: datetime.datetime | Unset = UNSET
    created_on: datetime.datetime | Unset = UNSET
    page: float | Unset = UNSET
    x: float | Unset = UNSET
    y: float | Unset = UNSET
    width: float | Unset = UNSET
    height: float | Unset = UNSET
    headers: list[str] | Unset = UNSET
    tags: list[TableTag] | Unset = UNSET
    rows: list[TableRow] | Unset = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        title = self.title

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

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

        page = self.page

        x = self.x

        y = self.y

        width = self.width

        height = self.height

        headers: list[str] | Unset = UNSET
        if not isinstance(self.headers, Unset):
            headers = self.headers

        tags: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.tags, Unset):
            tags = []
            for tags_item_data in self.tags:
                tags_item = tags_item_data.to_dict()
                tags.append(tags_item)

        rows: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.rows, Unset):
            rows = []
            for rows_item_data in self.rows:
                rows_item = rows_item_data.to_dict()
                rows.append(rows_item)

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update({})
        if raw is not UNSET:
            field_dict["raw"] = raw
        if title is not UNSET:
            field_dict["title"] = title
        if last_modified is not UNSET:
            field_dict["lastModified"] = last_modified
        if created_on is not UNSET:
            field_dict["createdON"] = created_on
        if page is not UNSET:
            field_dict["page"] = page
        if x is not UNSET:
            field_dict["x"] = x
        if y is not UNSET:
            field_dict["y"] = y
        if width is not UNSET:
            field_dict["width"] = width
        if height is not UNSET:
            field_dict["height"] = height
        if headers is not UNSET:
            field_dict["headers"] = headers
        if tags is not UNSET:
            field_dict["tags"] = tags
        if rows is not UNSET:
            field_dict["rows"] = rows

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.table_row import TableRow
        from ..models.table_tag import TableTag

        d = dict(src_dict)
        raw = d.pop("raw", UNSET)

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

        _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)

        _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)

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

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

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

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

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

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

        _tags = d.pop("tags", UNSET)
        tags: list[TableTag] | Unset = UNSET
        if _tags is not UNSET:
            tags = []
            for tags_item_data in _tags:
                tags_item = TableTag.from_dict(tags_item_data)

                tags.append(tags_item)

        _rows = d.pop("rows", UNSET)
        rows: list[TableRow] | Unset = UNSET
        if _rows is not UNSET:
            rows = []
            for rows_item_data in _rows:
                rows_item = TableRow.from_dict(rows_item_data)

                rows.append(rows_item)

        table = cls(
            raw=raw,
            title=title,
            last_modified=last_modified,
            created_on=created_on,
            page=page,
            x=x,
            y=y,
            width=width,
            height=height,
            headers=headers,
            tags=tags,
            rows=rows,
        )

        table.additional_properties = d
        return table

    @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
