# coding: utf-8

"""
    bxb API

    A comprehensive usage-based billing platform API. Manage customers, plans, subscriptions, invoices, payments, wallets, coupons, credit notes, taxes, webhooks, and more.

    The version of the OpenAPI document: 0.5.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, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from uuid import UUID
from bxb.models.credit_amount_cents import CreditAmountCents
from bxb.models.credit_note_item_create import CreditNoteItemCreate
from bxb.models.credit_note_reason import CreditNoteReason
from bxb.models.credit_note_type import CreditNoteType
from bxb.models.refund_amount_cents import RefundAmountCents
from bxb.models.taxes_amount_cents import TaxesAmountCents
from bxb.models.total_amount_cents import TotalAmountCents
from typing import Optional, Set
from typing_extensions import Self

class CreditNoteCreate(BaseModel):
    """
    CreditNoteCreate
    """ # noqa: E501
    number: Annotated[str, Field(strict=True, max_length=50)]
    invoice_id: UUID
    customer_id: UUID
    credit_note_type: CreditNoteType
    reason: CreditNoteReason
    description: Optional[StrictStr] = None
    credit_amount_cents: Optional[CreditAmountCents] = None
    refund_amount_cents: Optional[RefundAmountCents] = None
    total_amount_cents: Optional[TotalAmountCents] = None
    taxes_amount_cents: Optional[TaxesAmountCents] = None
    currency: Annotated[str, Field(min_length=3, strict=True, max_length=3)]
    items: Optional[List[CreditNoteItemCreate]] = None
    __properties: ClassVar[List[str]] = ["number", "invoice_id", "customer_id", "credit_note_type", "reason", "description", "credit_amount_cents", "refund_amount_cents", "total_amount_cents", "taxes_amount_cents", "currency", "items"]

    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 CreditNoteCreate 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 credit_amount_cents
        if self.credit_amount_cents:
            _dict['credit_amount_cents'] = self.credit_amount_cents.to_dict()
        # override the default output from pydantic by calling `to_dict()` of refund_amount_cents
        if self.refund_amount_cents:
            _dict['refund_amount_cents'] = self.refund_amount_cents.to_dict()
        # override the default output from pydantic by calling `to_dict()` of total_amount_cents
        if self.total_amount_cents:
            _dict['total_amount_cents'] = self.total_amount_cents.to_dict()
        # override the default output from pydantic by calling `to_dict()` of taxes_amount_cents
        if self.taxes_amount_cents:
            _dict['taxes_amount_cents'] = self.taxes_amount_cents.to_dict()
        # override the default output from pydantic by calling `to_dict()` of each item in items (list)
        _items = []
        if self.items:
            for _item_items in self.items:
                if _item_items:
                    _items.append(_item_items.to_dict())
            _dict['items'] = _items
        # set to None if description (nullable) is None
        # and model_fields_set contains the field
        if self.description is None and "description" in self.model_fields_set:
            _dict['description'] = None

        return _dict

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

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

        _obj = cls.model_validate({
            "number": obj.get("number"),
            "invoice_id": obj.get("invoice_id"),
            "customer_id": obj.get("customer_id"),
            "credit_note_type": obj.get("credit_note_type"),
            "reason": obj.get("reason"),
            "description": obj.get("description"),
            "credit_amount_cents": CreditAmountCents.from_dict(obj["credit_amount_cents"]) if obj.get("credit_amount_cents") is not None else None,
            "refund_amount_cents": RefundAmountCents.from_dict(obj["refund_amount_cents"]) if obj.get("refund_amount_cents") is not None else None,
            "total_amount_cents": TotalAmountCents.from_dict(obj["total_amount_cents"]) if obj.get("total_amount_cents") is not None else None,
            "taxes_amount_cents": TaxesAmountCents.from_dict(obj["taxes_amount_cents"]) if obj.get("taxes_amount_cents") is not None else None,
            "currency": obj.get("currency"),
            "items": [CreditNoteItemCreate.from_dict(_item) for _item in obj["items"]] if obj.get("items") is not None else None
        })
        return _obj


