# coding: utf-8

"""
Trading API

API for automated trading and exchange interface

The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictFloat, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional, Union
from crypticorn.trade.client.models.margin_mode import MarginMode
from crypticorn.trade.client.models.order_status import OrderStatus
from crypticorn.trade.client.models.trading_action_type import TradingActionType
from typing import Optional, Set
from typing_extensions import Self


class OrderModel(BaseModel):
    """
    Response model for orders. All optional as the model is built step by step.
    """  # noqa: E501

    created_at: Optional[StrictInt] = None
    updated_at: Optional[StrictInt] = None
    id: Optional[StrictStr] = None
    trading_action_id: Optional[StrictStr] = None
    execution_id: Optional[StrictStr] = None
    exchange_order_id: Optional[StrictStr] = None
    position_id: Optional[StrictStr] = None
    api_key_id: Optional[StrictStr] = None
    user_id: Optional[StrictStr] = None
    bot_id: Optional[StrictStr] = None
    client_order_id: Optional[StrictStr] = None
    exchange: Optional[StrictStr] = Field(
        default=None, description="Supported exchanges for trading"
    )
    symbol: Optional[StrictStr] = None
    common_symbol: Optional[StrictStr] = None
    price: Optional[Union[StrictFloat, StrictInt]] = None
    action_type: Optional[TradingActionType] = None
    market_type: Optional[StrictStr] = Field(default=None, description="Market types")
    margin_mode: Optional[MarginMode] = None
    status_code: Optional[StrictStr] = Field(
        default=None, description="API error identifiers"
    )
    status: Optional[OrderStatus] = None
    filled_perc: Optional[Union[StrictFloat, StrictInt]] = None
    filled_qty: Optional[Union[StrictFloat, StrictInt]] = None
    fee: Optional[Union[StrictFloat, StrictInt]] = None
    leverage: Optional[Union[StrictFloat, StrictInt]] = None
    order_details: Optional[Any] = Field(
        default=None, description="Exchange specific details of the order"
    )
    pnl: Optional[Union[StrictFloat, StrictInt]] = None
    order_time: Optional[StrictInt] = None
    __properties: ClassVar[List[str]] = [
        "created_at",
        "updated_at",
        "id",
        "trading_action_id",
        "execution_id",
        "exchange_order_id",
        "position_id",
        "api_key_id",
        "user_id",
        "bot_id",
        "client_order_id",
        "exchange",
        "symbol",
        "common_symbol",
        "price",
        "action_type",
        "market_type",
        "margin_mode",
        "status_code",
        "status",
        "filled_perc",
        "filled_qty",
        "fee",
        "leverage",
        "order_details",
        "pnl",
        "order_time",
    ]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )

    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of OrderModel from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of order_details
        if self.order_details:
            _dict["order_details"] = self.order_details.to_dict()
        # set to None if created_at (nullable) is None
        # and model_fields_set contains the field
        if self.created_at is None and "created_at" in self.model_fields_set:
            _dict["created_at"] = None

        # set to None if updated_at (nullable) is None
        # and model_fields_set contains the field
        if self.updated_at is None and "updated_at" in self.model_fields_set:
            _dict["updated_at"] = None

        # set to None if id (nullable) is None
        # and model_fields_set contains the field
        if self.id is None and "id" in self.model_fields_set:
            _dict["id"] = None

        # set to None if trading_action_id (nullable) is None
        # and model_fields_set contains the field
        if (
            self.trading_action_id is None
            and "trading_action_id" in self.model_fields_set
        ):
            _dict["trading_action_id"] = None

        # set to None if execution_id (nullable) is None
        # and model_fields_set contains the field
        if self.execution_id is None and "execution_id" in self.model_fields_set:
            _dict["execution_id"] = None

        # set to None if exchange_order_id (nullable) is None
        # and model_fields_set contains the field
        if (
            self.exchange_order_id is None
            and "exchange_order_id" in self.model_fields_set
        ):
            _dict["exchange_order_id"] = None

        # set to None if position_id (nullable) is None
        # and model_fields_set contains the field
        if self.position_id is None and "position_id" in self.model_fields_set:
            _dict["position_id"] = None

        # set to None if api_key_id (nullable) is None
        # and model_fields_set contains the field
        if self.api_key_id is None and "api_key_id" in self.model_fields_set:
            _dict["api_key_id"] = None

        # set to None if user_id (nullable) is None
        # and model_fields_set contains the field
        if self.user_id is None and "user_id" in self.model_fields_set:
            _dict["user_id"] = None

        # set to None if bot_id (nullable) is None
        # and model_fields_set contains the field
        if self.bot_id is None and "bot_id" in self.model_fields_set:
            _dict["bot_id"] = None

        # set to None if client_order_id (nullable) is None
        # and model_fields_set contains the field
        if self.client_order_id is None and "client_order_id" in self.model_fields_set:
            _dict["client_order_id"] = None

        # set to None if symbol (nullable) is None
        # and model_fields_set contains the field
        if self.symbol is None and "symbol" in self.model_fields_set:
            _dict["symbol"] = None

        # set to None if common_symbol (nullable) is None
        # and model_fields_set contains the field
        if self.common_symbol is None and "common_symbol" in self.model_fields_set:
            _dict["common_symbol"] = None

        # set to None if price (nullable) is None
        # and model_fields_set contains the field
        if self.price is None and "price" in self.model_fields_set:
            _dict["price"] = None

        # set to None if action_type (nullable) is None
        # and model_fields_set contains the field
        if self.action_type is None and "action_type" in self.model_fields_set:
            _dict["action_type"] = None

        # set to None if margin_mode (nullable) is None
        # and model_fields_set contains the field
        if self.margin_mode is None and "margin_mode" in self.model_fields_set:
            _dict["margin_mode"] = None

        # set to None if status (nullable) is None
        # and model_fields_set contains the field
        if self.status is None and "status" in self.model_fields_set:
            _dict["status"] = None

        # set to None if filled_perc (nullable) is None
        # and model_fields_set contains the field
        if self.filled_perc is None and "filled_perc" in self.model_fields_set:
            _dict["filled_perc"] = None

        # set to None if filled_qty (nullable) is None
        # and model_fields_set contains the field
        if self.filled_qty is None and "filled_qty" in self.model_fields_set:
            _dict["filled_qty"] = None

        # set to None if fee (nullable) is None
        # and model_fields_set contains the field
        if self.fee is None and "fee" in self.model_fields_set:
            _dict["fee"] = None

        # set to None if leverage (nullable) is None
        # and model_fields_set contains the field
        if self.leverage is None and "leverage" in self.model_fields_set:
            _dict["leverage"] = None

        # set to None if order_details (nullable) is None
        # and model_fields_set contains the field
        if self.order_details is None and "order_details" in self.model_fields_set:
            _dict["order_details"] = None

        # set to None if pnl (nullable) is None
        # and model_fields_set contains the field
        if self.pnl is None and "pnl" in self.model_fields_set:
            _dict["pnl"] = None

        # set to None if order_time (nullable) is None
        # and model_fields_set contains the field
        if self.order_time is None and "order_time" in self.model_fields_set:
            _dict["order_time"] = None

        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of OrderModel from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate(
            {
                "created_at": obj.get("created_at"),
                "updated_at": obj.get("updated_at"),
                "id": obj.get("id"),
                "trading_action_id": obj.get("trading_action_id"),
                "execution_id": obj.get("execution_id"),
                "exchange_order_id": obj.get("exchange_order_id"),
                "position_id": obj.get("position_id"),
                "api_key_id": obj.get("api_key_id"),
                "user_id": obj.get("user_id"),
                "bot_id": obj.get("bot_id"),
                "client_order_id": obj.get("client_order_id"),
                "exchange": obj.get("exchange"),
                "symbol": obj.get("symbol"),
                "common_symbol": obj.get("common_symbol"),
                "price": obj.get("price"),
                "action_type": obj.get("action_type"),
                "market_type": obj.get("market_type"),
                "margin_mode": obj.get("margin_mode"),
                "status_code": obj.get("status_code"),
                "status": obj.get("status"),
                "filled_perc": obj.get("filled_perc"),
                "filled_qty": obj.get("filled_qty"),
                "fee": obj.get("fee"),
                "leverage": obj.get("leverage"),
                "order_details": (
                    AnyOf.from_dict(obj["order_details"])
                    if obj.get("order_details") is not None
                    else None
                ),
                "pnl": obj.get("pnl"),
                "order_time": obj.get("order_time"),
            }
        )
        return _obj
