# coding: utf-8

"""
DEX AI API

API for DEX AI

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 json
import pprint
import re  # noqa: F401
from typing import Any, ClassVar, Dict, List, Optional, Set, Union

from pydantic import BaseModel, ConfigDict, Field, StrictFloat, StrictInt, StrictStr
from typing_extensions import Self

from crypticorn.dex.client.models.signal_volume import SignalVolume
from crypticorn.dex.client.models.token_data import TokenData


class SignalWithToken(BaseModel):
    """
    Trading signal enriched with comprehensive token metadata and information.
    """  # noqa: E501

    ca: StrictStr = Field(description="The contract address of the token")
    name: StrictStr = Field(description="The name of the token")
    type: StrictStr = Field(description="The type of the signal")
    risk: Optional[StrictStr] = None
    performance: StrictStr = Field(
        description="The performance in percent as a string. e.g. +100%"
    )
    price: StrictStr = Field(description="The price of the signal")
    volume: SignalVolume = Field(description="The volume of the signal")
    liquidity: StrictStr = Field(description="The liquidity of the signal")
    mcap: StrictStr = Field(description="The market cap of the signal")
    called_at: StrictInt = Field(description="The timestamp of the call")
    updated_at: StrictInt = Field(description="The timestamp of the last update")
    tg_id: Optional[StrictStr]
    data: TokenData = Field(description="The token info")
    performance_float: Union[StrictFloat, StrictInt] = Field(
        description="The performance in float. e.g. +100% -> 2.0"
    )
    __properties: ClassVar[List[str]] = [
        "ca",
        "name",
        "type",
        "risk",
        "performance",
        "price",
        "volume",
        "liquidity",
        "mcap",
        "called_at",
        "updated_at",
        "tg_id",
        "data",
        "performance_float",
    ]

    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 SignalWithToken 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.
        * OpenAPI `readOnly` fields are excluded.
        """
        excluded_fields: Set[str] = set(
            [
                "performance_float",
            ]
        )

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

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

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "ca": obj.get("ca"),
                "name": obj.get("name"),
                "type": obj.get("type"),
                "risk": obj.get("risk"),
                "performance": obj.get("performance"),
                "price": obj.get("price"),
                "volume": (
                    SignalVolume.from_dict(obj["volume"])
                    if obj.get("volume") is not None
                    else None
                ),
                "liquidity": obj.get("liquidity"),
                "mcap": obj.get("mcap"),
                "called_at": obj.get("called_at"),
                "updated_at": obj.get("updated_at"),
                "tg_id": obj.get("tg_id"),
                "data": (
                    TokenData.from_dict(obj["data"])
                    if obj.get("data") is not None
                    else None
                ),
                "performance_float": obj.get("performance_float"),
            }
        )
        return _obj
