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.history_change import HistoryChange
    from ..models.tpa_statement_accepted import TpaStatementAccepted
    from ..models.tpa_statement_confidence import TpaStatementConfidence
    from ..models.tpa_statement_ref import TpaStatementRef
    from ..models.transaction_diff import TransactionDiff
    from ..models.transaction_summary import TransactionSummary


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


@_attrs_define
class TpaStatement:
    """
    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):
        document (str | Unset): MongoDB ObjectId
        page_count (float | Unset):
        duplicates (list[str] | Unset):
        creator (str | Unset): MongoDB ObjectId
        plan_year (float | Unset):
        period_start (datetime.datetime | Unset):
        period_end (datetime.datetime | Unset):
        months (list[str] | Unset):
        beginning_balance (float | Unset):
        ending_balance (float | Unset):
        reconciles (bool | Unset):
        tx (TransactionSummary | Unset):
        diff (TransactionDiff | Unset):
        errors (list[str] | Unset):
        confidence (TpaStatementConfidence | Unset):
        ref (TpaStatementRef | Unset):
        participant (str | Unset): MongoDB ObjectId
        money_type (str | Unset):
        confirmed (bool | Unset):
        client_confirmed (bool | Unset):
        accepted (TpaStatementAccepted | Unset):
        history (list[HistoryChange] | Unset):
        created_on (datetime.datetime | Unset):
        last_modified (datetime.datetime | Unset):
        classifying (bool | 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
    document: str | Unset = UNSET
    page_count: float | Unset = UNSET
    duplicates: list[str] | Unset = UNSET
    creator: str | Unset = UNSET
    plan_year: float | Unset = UNSET
    period_start: datetime.datetime | Unset = UNSET
    period_end: datetime.datetime | Unset = UNSET
    months: list[str] | Unset = UNSET
    beginning_balance: float | Unset = UNSET
    ending_balance: float | Unset = UNSET
    reconciles: bool | Unset = UNSET
    tx: TransactionSummary | Unset = UNSET
    diff: TransactionDiff | Unset = UNSET
    errors: list[str] | Unset = UNSET
    confidence: TpaStatementConfidence | Unset = UNSET
    ref: TpaStatementRef | Unset = UNSET
    participant: str | Unset = UNSET
    money_type: str | Unset = UNSET
    confirmed: bool | Unset = UNSET
    client_confirmed: bool | Unset = UNSET
    accepted: TpaStatementAccepted | Unset = UNSET
    history: list[HistoryChange] | Unset = UNSET
    created_on: datetime.datetime | Unset = UNSET
    last_modified: datetime.datetime | Unset = UNSET
    classifying: bool | 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

        document = self.document

        page_count = self.page_count

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

        creator = self.creator

        plan_year = self.plan_year

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

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

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

        beginning_balance = self.beginning_balance

        ending_balance = self.ending_balance

        reconciles = self.reconciles

        tx: dict[str, Any] | Unset = UNSET
        if not isinstance(self.tx, Unset):
            tx = self.tx.to_dict()

        diff: dict[str, Any] | Unset = UNSET
        if not isinstance(self.diff, Unset):
            diff = self.diff.to_dict()

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

        confidence: dict[str, Any] | Unset = UNSET
        if not isinstance(self.confidence, Unset):
            confidence = self.confidence.to_dict()

        ref: dict[str, Any] | Unset = UNSET
        if not isinstance(self.ref, Unset):
            ref = self.ref.to_dict()

        participant = self.participant

        money_type = self.money_type

        confirmed = self.confirmed

        client_confirmed = self.client_confirmed

        accepted: dict[str, Any] | Unset = UNSET
        if not isinstance(self.accepted, Unset):
            accepted = self.accepted.to_dict()

        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)

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

        classifying = self.classifying

        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 document is not UNSET:
            field_dict["document"] = document
        if page_count is not UNSET:
            field_dict["pageCount"] = page_count
        if duplicates is not UNSET:
            field_dict["duplicates"] = duplicates
        if creator is not UNSET:
            field_dict["creator"] = creator
        if plan_year is not UNSET:
            field_dict["planYear"] = plan_year
        if period_start is not UNSET:
            field_dict["periodStart"] = period_start
        if period_end is not UNSET:
            field_dict["periodEnd"] = period_end
        if months is not UNSET:
            field_dict["months"] = months
        if beginning_balance is not UNSET:
            field_dict["beginningBalance"] = beginning_balance
        if ending_balance is not UNSET:
            field_dict["endingBalance"] = ending_balance
        if reconciles is not UNSET:
            field_dict["reconciles"] = reconciles
        if tx is not UNSET:
            field_dict["tx"] = tx
        if diff is not UNSET:
            field_dict["diff"] = diff
        if errors is not UNSET:
            field_dict["errors"] = errors
        if confidence is not UNSET:
            field_dict["confidence"] = confidence
        if ref is not UNSET:
            field_dict["ref"] = ref
        if participant is not UNSET:
            field_dict["participant"] = participant
        if money_type is not UNSET:
            field_dict["moneyType"] = money_type
        if confirmed is not UNSET:
            field_dict["confirmed"] = confirmed
        if client_confirmed is not UNSET:
            field_dict["clientConfirmed"] = client_confirmed
        if accepted is not UNSET:
            field_dict["accepted"] = accepted
        if history is not UNSET:
            field_dict["history"] = history
        if created_on is not UNSET:
            field_dict["createdOn"] = created_on
        if last_modified is not UNSET:
            field_dict["lastModified"] = last_modified
        if classifying is not UNSET:
            field_dict["classifying"] = classifying

        return field_dict

    @classmethod
    def from_dict(cls: type[T], src_dict: Mapping[str, Any]) -> T:
        from ..models.history_change import HistoryChange
        from ..models.tpa_statement_accepted import TpaStatementAccepted
        from ..models.tpa_statement_confidence import TpaStatementConfidence
        from ..models.tpa_statement_ref import TpaStatementRef
        from ..models.transaction_diff import TransactionDiff
        from ..models.transaction_summary import TransactionSummary

        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)

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

        page_count = d.pop("pageCount", UNSET)

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

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

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

        _period_start = d.pop("periodStart", UNSET)
        period_start: datetime.datetime | Unset
        if isinstance(_period_start, Unset):
            period_start = UNSET
        else:
            period_start = isoparse(_period_start)

        _period_end = d.pop("periodEnd", UNSET)
        period_end: datetime.datetime | Unset
        if isinstance(_period_end, Unset):
            period_end = UNSET
        else:
            period_end = isoparse(_period_end)

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

        beginning_balance = d.pop("beginningBalance", UNSET)

        ending_balance = d.pop("endingBalance", UNSET)

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

        _tx = d.pop("tx", UNSET)
        tx: TransactionSummary | Unset
        if isinstance(_tx, Unset):
            tx = UNSET
        else:
            tx = TransactionSummary.from_dict(_tx)

        _diff = d.pop("diff", UNSET)
        diff: TransactionDiff | Unset
        if isinstance(_diff, Unset):
            diff = UNSET
        else:
            diff = TransactionDiff.from_dict(_diff)

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

        _confidence = d.pop("confidence", UNSET)
        confidence: TpaStatementConfidence | Unset
        if isinstance(_confidence, Unset):
            confidence = UNSET
        else:
            confidence = TpaStatementConfidence.from_dict(_confidence)

        _ref = d.pop("ref", UNSET)
        ref: TpaStatementRef | Unset
        if isinstance(_ref, Unset):
            ref = UNSET
        else:
            ref = TpaStatementRef.from_dict(_ref)

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

        money_type = d.pop("moneyType", UNSET)

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

        client_confirmed = d.pop("clientConfirmed", UNSET)

        _accepted = d.pop("accepted", UNSET)
        accepted: TpaStatementAccepted | Unset
        if isinstance(_accepted, Unset):
            accepted = UNSET
        else:
            accepted = TpaStatementAccepted.from_dict(_accepted)

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

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

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

        tpa_statement = cls(
            field_id=field_id,
            team=team,
            plan=plan,
            account=account,
            plan_id=plan_id,
            account_number=account_number,
            document=document,
            page_count=page_count,
            duplicates=duplicates,
            creator=creator,
            plan_year=plan_year,
            period_start=period_start,
            period_end=period_end,
            months=months,
            beginning_balance=beginning_balance,
            ending_balance=ending_balance,
            reconciles=reconciles,
            tx=tx,
            diff=diff,
            errors=errors,
            confidence=confidence,
            ref=ref,
            participant=participant,
            money_type=money_type,
            confirmed=confirmed,
            client_confirmed=client_confirmed,
            accepted=accepted,
            history=history,
            created_on=created_on,
            last_modified=last_modified,
            classifying=classifying,
        )

        tpa_statement.additional_properties = d
        return tpa_statement

    @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
