# coding: utf-8

"""
    PAY.JP API v2

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: 2.0.0
    Contact: support@pay.jp
    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 datetime import datetime
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictInt, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from payjpv2.models.balance_state import BalanceState
from payjpv2.models.bank_info_response import BankInfoResponse
from payjpv2.models.statement_response import StatementResponse
from typing import Optional, Set
from typing_extensions import Self

class BalanceResponse(BaseModel):
    """
    BalanceResponse
    """ # noqa: E501
    object: Optional[StrictStr] = 'balance'
    id: StrictStr = Field(description="残高 ID")
    livemode: StrictBool = Field(description="本番環境かどうか")
    state: BalanceState = Field(description="Balance の状態  | 値 | |:---| | **collecting**: 集計中 | | **transfer**: 入金 | | **claim**: 請求 |")
    statements: List[StatementResponse] = Field(description="関連付けられている Statement オブジェクトのリスト")
    closed: StrictBool = Field(description="この Balance の清算が終了していれば true  state=transfer であれば加盟店口座への入金作業完了、state=claim であれば PAY.JP で請求額の振込が確認できたことを表します。")
    closed_date: Optional[datetime]
    due_date: Optional[datetime]
    net: StrictInt = Field(description="関連付けられている Statement の総額")
    bank_info: Optional[BankInfoResponse]
    created_at: datetime = Field(description="作成日時 (UTC, ISO 8601 形式)")
    updated_at: datetime = Field(description="更新時の日時 (UTC, ISO 8601 形式)")
    __properties: ClassVar[List[str]] = ["object", "id", "livemode", "state", "statements", "closed", "closed_date", "due_date", "net", "bank_info", "created_at", "updated_at"]

    @field_validator('object')
    def object_validate_enum(cls, value):
        """Validates the enum"""
        if value is None:
            return value

        if value not in set(['balance']):
            raise ValueError("must be one of enum values ('balance')")
        return value

    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"""
        return json.dumps(self.model_dump(by_alias=True, exclude_unset=True))

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of BalanceResponse 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 each item in statements (list)
        _items = []
        if self.statements:
            for _item_statements in self.statements:
                if _item_statements:
                    _items.append(_item_statements.to_dict())
            _dict['statements'] = _items
        # override the default output from pydantic by calling `to_dict()` of bank_info
        if self.bank_info:
            _dict['bank_info'] = self.bank_info.to_dict()
        # set to None if closed_date (nullable) is None
        # and model_fields_set contains the field
        if self.closed_date is None and "closed_date" in self.model_fields_set:
            _dict['closed_date'] = None

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "object": obj.get("object") if obj.get("object") is not None else 'balance',
            "id": obj.get("id"),
            "livemode": obj.get("livemode"),
            "state": obj.get("state"),
            "statements": [StatementResponse.from_dict(_item) for _item in obj["statements"]] if obj.get("statements") is not None else None,
            "closed": obj.get("closed"),
            "closed_date": obj.get("closed_date"),
            "due_date": obj.get("due_date"),
            "net": obj.get("net"),
            "bank_info": BankInfoResponse.from_dict(obj["bank_info"]) if obj.get("bank_info") is not None else None,
            "created_at": obj.get("created_at"),
            "updated_at": obj.get("updated_at")
        })
        return _obj


