# coding: utf-8

"""
    FlexPrice API

    FlexPrice API Service

    The version of the OpenAPI document: 1.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, StrictBool, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from flexprice.models.dto_create_credit_note_line_item_request import DtoCreateCreditNoteLineItemRequest
from flexprice.models.types_credit_note_reason import TypesCreditNoteReason
from typing import Optional, Set
from typing_extensions import Self

class DtoCreateCreditNoteRequest(BaseModel):
    """
    DtoCreateCreditNoteRequest
    """ # noqa: E501
    credit_note_number: Optional[StrictStr] = Field(default=None, description="credit_note_number is an optional human-readable identifier for the credit note")
    idempotency_key: Optional[StrictStr] = Field(default=None, description="idempotency_key is an optional key used to prevent duplicate credit note creation")
    invoice_id: StrictStr = Field(description="invoice_id is the unique identifier of the invoice this credit note is applied to")
    line_items: Optional[List[DtoCreateCreditNoteLineItemRequest]] = Field(default=None, description="line_items contains the individual line items that make up this credit note (minimum 1 required)")
    memo: Optional[StrictStr] = Field(default=None, description="memo is an optional free-text field for additional notes about the credit note")
    metadata: Optional[Dict[str, StrictStr]] = None
    process_credit_note: Optional[StrictBool] = Field(default=True, description="process_credit_note is a flag to process the credit note after creation")
    reason: TypesCreditNoteReason
    __properties: ClassVar[List[str]] = ["credit_note_number", "idempotency_key", "invoice_id", "line_items", "memo", "metadata", "process_credit_note", "reason"]

    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 DtoCreateCreditNoteRequest 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 line_items (list)
        _items = []
        if self.line_items:
            for _item_line_items in self.line_items:
                if _item_line_items:
                    _items.append(_item_line_items.to_dict())
            _dict['line_items'] = _items
        return _dict

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

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

        _obj = cls.model_validate({
            "credit_note_number": obj.get("credit_note_number"),
            "idempotency_key": obj.get("idempotency_key"),
            "invoice_id": obj.get("invoice_id"),
            "line_items": [DtoCreateCreditNoteLineItemRequest.from_dict(_item) for _item in obj["line_items"]] if obj.get("line_items") is not None else None,
            "memo": obj.get("memo"),
            "metadata": obj.get("metadata"),
            "process_credit_note": obj.get("process_credit_note") if obj.get("process_credit_note") is not None else True,
            "reason": obj.get("reason")
        })
        return _obj


