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.history_change import HistoryChange


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


@_attrs_define
class TpaTransaction:
    """
    Attributes:
        field_id (str | Unset): MongoDB ObjectId
        team (str | Unset): MongoDB ObjectId
        plan (str | Unset): MongoDB ObjectId
        account (str | Unset): MongoDB ObjectId
        plan_id (str | Unset):
        account_number (str | Unset):
        statement (str | Unset): MongoDB ObjectId
        row (str | Unset): MongoDB ObjectId
        creator (str | Unset): MongoDB ObjectId
        plan_year (float | Unset):
        page (float | Unset):
        date (datetime.datetime | Unset):
        description (str | Unset):
        amount (float | Unset):
        tx_type (str | Unset):
        tx_sub_type (str | Unset):
        confidence (float | Unset):
        history (list[HistoryChange] | Unset):
    """

    field_id: str | Unset = UNSET
    team: str | Unset = UNSET
    plan: str | Unset = UNSET
    account: str | Unset = UNSET
    plan_id: str | Unset = UNSET
    account_number: str | Unset = UNSET
    statement: str | Unset = UNSET
    row: str | Unset = UNSET
    creator: str | Unset = UNSET
    plan_year: float | Unset = UNSET
    page: float | Unset = UNSET
    date: datetime.datetime | Unset = UNSET
    description: str | Unset = UNSET
    amount: float | Unset = UNSET
    tx_type: str | Unset = UNSET
    tx_sub_type: str | Unset = UNSET
    confidence: float | Unset = UNSET
    history: list[HistoryChange] | 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

        plan = self.plan

        account = self.account

        plan_id = self.plan_id

        account_number = self.account_number

        statement = self.statement

        row = self.row

        creator = self.creator

        plan_year = self.plan_year

        page = self.page

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

        description = self.description

        amount = self.amount

        tx_type = self.tx_type

        tx_sub_type = self.tx_sub_type

        confidence = self.confidence

        history: list[dict[str, Any]] | Unset = UNSET
        if not isinstance(self.history, Unset):
            history = []
            for history_item_data in self.history:
                history_item = history_item_data.to_dict()
                history.append(history_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 plan is not UNSET:
            field_dict["plan"] = plan
        if account is not UNSET:
            field_dict["account"] = account
        if plan_id is not UNSET:
            field_dict["planId"] = plan_id
        if account_number is not UNSET:
            field_dict["accountNumber"] = account_number
        if statement is not UNSET:
            field_dict["statement"] = statement
        if row is not UNSET:
            field_dict["row"] = row
        if creator is not UNSET:
            field_dict["creator"] = creator
        if plan_year is not UNSET:
            field_dict["planYear"] = plan_year
        if page is not UNSET:
            field_dict["page"] = page
        if date is not UNSET:
            field_dict["date"] = date
        if description is not UNSET:
            field_dict["description"] = description
        if amount is not UNSET:
            field_dict["amount"] = amount
        if tx_type is not UNSET:
            field_dict["txType"] = tx_type
        if tx_sub_type is not UNSET:
            field_dict["txSubType"] = tx_sub_type
        if confidence is not UNSET:
            field_dict["confidence"] = confidence
        if history is not UNSET:
            field_dict["history"] = history

        return field_dict

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

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

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

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

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

        plan_id = d.pop("planId", UNSET)

        account_number = d.pop("accountNumber", UNSET)

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

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

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

        plan_year = d.pop("planYear", UNSET)

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

        _date = d.pop("date", UNSET)
        date: datetime.datetime | Unset
        if isinstance(_date, Unset):
            date = UNSET
        else:
            date = isoparse(_date)

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

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

        tx_type = d.pop("txType", UNSET)

        tx_sub_type = d.pop("txSubType", UNSET)

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

        _history = d.pop("history", UNSET)
        history: list[HistoryChange] | Unset = UNSET
        if _history is not UNSET:
            history = []
            for history_item_data in _history:
                history_item = HistoryChange.from_dict(history_item_data)

                history.append(history_item)

        tpa_transaction = cls(
            field_id=field_id,
            team=team,
            plan=plan,
            account=account,
            plan_id=plan_id,
            account_number=account_number,
            statement=statement,
            row=row,
            creator=creator,
            plan_year=plan_year,
            page=page,
            date=date,
            description=description,
            amount=amount,
            tx_type=tx_type,
            tx_sub_type=tx_sub_type,
            confidence=confidence,
            history=history,
        )

        tpa_transaction.additional_properties = d
        return tpa_transaction

    @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
