# coding: utf-8

"""
    QuantDB API

    Financial data API service using core business logic

    The version of the OpenAPI document: 
    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
from typing import Any, ClassVar, Dict, List, Optional, Union
from jinyanniu_databases_quantdb_client.models.model_date import ModelDate
from typing import Optional, Set
from typing_extensions import Self
from pydantic_core import to_jsonable_python

class HistoricalDataPoint(BaseModel):
    """
    Schema for a single historical data point
    """ # noqa: E501
    var_date: ModelDate = Field(alias="date")
    open: Optional[Union[StrictFloat, StrictInt]] = None
    high: Optional[Union[StrictFloat, StrictInt]] = None
    low: Optional[Union[StrictFloat, StrictInt]] = None
    close: Optional[Union[StrictFloat, StrictInt]] = None
    volume: Optional[Union[StrictFloat, StrictInt]] = None
    turnover: Optional[Union[StrictFloat, StrictInt]] = None
    amplitude: Optional[Union[StrictFloat, StrictInt]] = None
    pct_change: Optional[Union[StrictFloat, StrictInt]] = None
    change: Optional[Union[StrictFloat, StrictInt]] = None
    turnover_rate: Optional[Union[StrictFloat, StrictInt]] = None
    __properties: ClassVar[List[str]] = ["date", "open", "high", "low", "close", "volume", "turnover", "amplitude", "pct_change", "change", "turnover_rate"]

    model_config = ConfigDict(
        validate_by_name=True,
        validate_by_alias=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"""
        return json.dumps(to_jsonable_python(self.to_dict()))

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of HistoricalDataPoint 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 var_date
        if self.var_date:
            _dict['date'] = self.var_date.to_dict()
        # set to None if open (nullable) is None
        # and model_fields_set contains the field
        if self.open is None and "open" in self.model_fields_set:
            _dict['open'] = None

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

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

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

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

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "date": ModelDate.from_dict(obj["date"]) if obj.get("date") is not None else None,
            "open": obj.get("open"),
            "high": obj.get("high"),
            "low": obj.get("low"),
            "close": obj.get("close"),
            "volume": obj.get("volume"),
            "turnover": obj.get("turnover"),
            "amplitude": obj.get("amplitude"),
            "pct_change": obj.get("pct_change"),
            "change": obj.get("change"),
            "turnover_rate": obj.get("turnover_rate")
        })
        return _obj


