# coding: utf-8

"""
    BoxBilling

    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.6.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 datetime import datetime
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from bxb.models.amount_cents1 import AmountCents1
from bxb.models.coupon_expiration import CouponExpiration
from bxb.models.coupon_frequency import CouponFrequency
from bxb.models.coupon_type import CouponType
from bxb.models.percentage_rate import PercentageRate
from typing import Optional, Set
from typing_extensions import Self

class CouponCreate(BaseModel):
    """
    CouponCreate
    """ # noqa: E501
    code: Annotated[str, Field(strict=True, max_length=255)]
    name: Annotated[str, Field(strict=True, max_length=255)]
    description: Optional[StrictStr] = None
    coupon_type: CouponType
    amount_cents: Optional[AmountCents1] = None
    amount_currency: Optional[Annotated[str, Field(min_length=3, strict=True, max_length=3)]] = None
    percentage_rate: Optional[PercentageRate] = None
    frequency: CouponFrequency
    frequency_duration: Optional[StrictInt] = None
    reusable: Optional[StrictBool] = True
    expiration: Optional[CouponExpiration] = None
    expiration_at: Optional[datetime] = None
    __properties: ClassVar[List[str]] = ["code", "name", "description", "coupon_type", "amount_cents", "amount_currency", "percentage_rate", "frequency", "frequency_duration", "reusable", "expiration", "expiration_at"]

    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 CouponCreate 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 amount_cents
        if self.amount_cents:
            _dict['amount_cents'] = self.amount_cents.to_dict()
        # override the default output from pydantic by calling `to_dict()` of percentage_rate
        if self.percentage_rate:
            _dict['percentage_rate'] = self.percentage_rate.to_dict()
        # 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

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "code": obj.get("code"),
            "name": obj.get("name"),
            "description": obj.get("description"),
            "coupon_type": obj.get("coupon_type"),
            "amount_cents": AmountCents1.from_dict(obj["amount_cents"]) if obj.get("amount_cents") is not None else None,
            "amount_currency": obj.get("amount_currency"),
            "percentage_rate": PercentageRate.from_dict(obj["percentage_rate"]) if obj.get("percentage_rate") is not None else None,
            "frequency": obj.get("frequency"),
            "frequency_duration": obj.get("frequency_duration"),
            "reusable": obj.get("reusable") if obj.get("reusable") is not None else True,
            "expiration": obj.get("expiration"),
            "expiration_at": obj.get("expiration_at")
        })
        return _obj


