# Copyright 2025 21X AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import datetime
from collections.abc import Mapping
from typing import Any, TypeVar, Union

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

from ..models.order_kind_enum import OrderKindEnum
from ..types import UNSET, Unset

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


@_attrs_define
class Trade:
    """
    Attributes:
        id (str):
        transaction_type (OrderKindEnum):
        base_token_symbol (str): The symbol of the financial instrument token that was traded
        quote_token_symbol (str): The e-money token that was used in the trade
        base_token_quantity (str): The number of financial instrument tokens that were transferred
        quote_token_quantity (str): The number of e-money tokens that were transferred (excluding commission)
        price (str): The price point at which the trade was executed
        sequence_no (Union[Unset, int]): Trade number generated by the order book
        transaction_date (Union[Unset, datetime.datetime]): The time at which the corresponding block-chain block was
            generated
        finality_date (Union[Unset, datetime.datetime]): The time since when the transaction is considered final by 21X.
        commission (Union[Unset, str]): The number of e-money tokens the participant paid as commission
        order_id (Union[Unset, str]): Link to the participant's order involved in the trade
        transaction_hash (Union[Unset, str]): Unique identifier of the corresponding blockchain transaction
    """

    id: str
    transaction_type: OrderKindEnum
    base_token_symbol: str
    quote_token_symbol: str
    base_token_quantity: str
    quote_token_quantity: str
    price: str
    sequence_no: Union[Unset, int] = UNSET
    transaction_date: Union[Unset, datetime.datetime] = UNSET
    finality_date: Union[Unset, datetime.datetime] = UNSET
    commission: Union[Unset, str] = UNSET
    order_id: Union[Unset, str] = UNSET
    transaction_hash: Union[Unset, str] = UNSET
    additional_properties: dict[str, Any] = _attrs_field(init=False, factory=dict)

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

        transaction_type = self.transaction_type.value

        base_token_symbol = self.base_token_symbol

        quote_token_symbol = self.quote_token_symbol

        base_token_quantity = self.base_token_quantity

        quote_token_quantity = self.quote_token_quantity

        price = self.price

        sequence_no = self.sequence_no

        transaction_date: Union[Unset, str] = UNSET
        if not isinstance(self.transaction_date, Unset):
            transaction_date = self.transaction_date.isoformat()

        finality_date: Union[Unset, str] = UNSET
        if not isinstance(self.finality_date, Unset):
            finality_date = self.finality_date.isoformat()

        commission = self.commission

        order_id = self.order_id

        transaction_hash = self.transaction_hash

        field_dict: dict[str, Any] = {}
        field_dict.update(self.additional_properties)
        field_dict.update(
            {
                "id": id,
                "transactionType": transaction_type,
                "baseTokenSymbol": base_token_symbol,
                "quoteTokenSymbol": quote_token_symbol,
                "baseTokenQuantity": base_token_quantity,
                "quoteTokenQuantity": quote_token_quantity,
                "price": price,
            }
        )
        if sequence_no is not UNSET:
            field_dict["sequenceNo"] = sequence_no
        if transaction_date is not UNSET:
            field_dict["transactionDate"] = transaction_date
        if finality_date is not UNSET:
            field_dict["finalityDate"] = finality_date
        if commission is not UNSET:
            field_dict["commission"] = commission
        if order_id is not UNSET:
            field_dict["orderId"] = order_id
        if transaction_hash is not UNSET:
            field_dict["transactionHash"] = transaction_hash

        return field_dict

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

        transaction_type = OrderKindEnum(d.pop("transactionType"))

        base_token_symbol = d.pop("baseTokenSymbol")

        quote_token_symbol = d.pop("quoteTokenSymbol")

        base_token_quantity = d.pop("baseTokenQuantity")

        quote_token_quantity = d.pop("quoteTokenQuantity")

        price = d.pop("price")

        sequence_no = d.pop("sequenceNo", UNSET)

        _transaction_date = d.pop("transactionDate", UNSET)
        transaction_date: Union[Unset, datetime.datetime]
        if isinstance(_transaction_date, Unset):
            transaction_date = UNSET
        else:
            transaction_date = isoparse(_transaction_date)

        _finality_date = d.pop("finalityDate", UNSET)
        finality_date: Union[Unset, datetime.datetime]
        if isinstance(_finality_date, Unset):
            finality_date = UNSET
        else:
            finality_date = isoparse(_finality_date)

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

        order_id = d.pop("orderId", UNSET)

        transaction_hash = d.pop("transactionHash", UNSET)

        trade = cls(
            id=id,
            transaction_type=transaction_type,
            base_token_symbol=base_token_symbol,
            quote_token_symbol=quote_token_symbol,
            base_token_quantity=base_token_quantity,
            quote_token_quantity=quote_token_quantity,
            price=price,
            sequence_no=sequence_no,
            transaction_date=transaction_date,
            finality_date=finality_date,
            commission=commission,
            order_id=order_id,
            transaction_hash=transaction_hash,
        )

        trade.additional_properties = d
        return trade

    @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
