# coding: utf-8

"""
DEX AI API

API for DEX AI

The version of the OpenAPI document: 2.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.dex.client.models.info import Info
from crypticorn.dex.client.models.liquidity import Liquidity
from crypticorn.dex.client.models.price_change import PriceChange
from crypticorn.dex.client.models.token import Token
from crypticorn.dex.client.models.txns import Txns
from crypticorn.dex.client.models.volume import Volume
from typing import Set
from typing_extensions import Self


class TokenData(BaseModel):
    """
    TokenData
    """  # noqa: E501

    chain_id: StrictStr = Field(description="Blockchain identifier")
    dex_id: StrictStr = Field(description="DEX identifier")
    url: StrictStr = Field(description="DexScreener pair URL")
    pair_address: StrictStr = Field(description="Pair/pool address")
    base_token: Token = Field(description="Base token metadata")
    quote_token: Token = Field(description="Quote token metadata")
    price_native: Union[StrictFloat, StrictInt] = Field(
        description="Token price in native chain currency"
    )
    price_usd: Union[StrictFloat, StrictInt] = Field(description="Token price in USD")
    txns: Txns = Field(
        description="Buy/sell transaction counts over multiple time windows"
    )
    volume: Volume = Field(description="Trading volume over multiple time windows")
    price_change: PriceChange = Field(
        description="Price change percentages over multiple time windows"
    )
    liquidity: Optional[Liquidity] = Field(
        default=None, description="Liquidity breakdown for the pair"
    )
    fdv: Union[StrictFloat, StrictInt] = Field(
        description="Fully diluted valuation in USD"
    )
    market_cap: Union[StrictFloat, StrictInt] = Field(
        description="Market capitalization in USD"
    )
    pair_created_at: StrictInt = Field(
        description="Pair creation timestamp (milliseconds since epoch)"
    )
    info: Optional[Info] = None
    __properties: ClassVar[List[str]] = [
        "chain_id",
        "dex_id",
        "url",
        "pair_address",
        "base_token",
        "quote_token",
        "price_native",
        "price_usd",
        "txns",
        "volume",
        "price_change",
        "liquidity",
        "fdv",
        "market_cap",
        "pair_created_at",
        "info",
    ]

    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 TokenData 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 base_token
        if self.base_token:
            _dict["base_token"] = self.base_token.to_dict()
        # override the default output from pydantic by calling `to_dict()` of quote_token
        if self.quote_token:
            _dict["quote_token"] = self.quote_token.to_dict()
        # override the default output from pydantic by calling `to_dict()` of txns
        if self.txns:
            _dict["txns"] = self.txns.to_dict()
        # 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 price_change
        if self.price_change:
            _dict["price_change"] = self.price_change.to_dict()
        # override the default output from pydantic by calling `to_dict()` of liquidity
        if self.liquidity:
            _dict["liquidity"] = self.liquidity.to_dict()
        # override the default output from pydantic by calling `to_dict()` of info
        if self.info:
            _dict["info"] = self.info.to_dict()
        # set to None if info (nullable) is None
        # and model_fields_set contains the field
        if self.info is None and "info" in self.model_fields_set:
            _dict["info"] = None

        return _dict

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

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

        _obj = cls.model_validate(
            {
                "chain_id": obj.get("chain_id"),
                "dex_id": obj.get("dex_id"),
                "url": obj.get("url"),
                "pair_address": obj.get("pair_address"),
                "base_token": Token.from_dict(obj["base_token"])
                if obj.get("base_token") is not None
                else None,
                "quote_token": Token.from_dict(obj["quote_token"])
                if obj.get("quote_token") is not None
                else None,
                "price_native": obj.get("price_native"),
                "price_usd": obj.get("price_usd"),
                "txns": Txns.from_dict(obj["txns"])
                if obj.get("txns") is not None
                else None,
                "volume": Volume.from_dict(obj["volume"])
                if obj.get("volume") is not None
                else None,
                "price_change": PriceChange.from_dict(obj["price_change"])
                if obj.get("price_change") is not None
                else None,
                "liquidity": Liquidity.from_dict(obj["liquidity"])
                if obj.get("liquidity") is not None
                else None,
                "fdv": obj.get("fdv"),
                "market_cap": obj.get("market_cap"),
                "pair_created_at": obj.get("pair_created_at"),
                "info": Info.from_dict(obj["info"])
                if obj.get("info") is not None
                else None,
            }
        )
        return _obj
