# coding: utf-8

"""
    Voucherify API

    Voucherify promotion engine REST API. Please see https://docs.voucherify.io/docs for more details.

    The version of the OpenAPI document: v2018-08-01
    Contact: support@voucherify.io
    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
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from voucherify.models.bundle_identified_item import BundleIdentifiedItem
from voucherify.models.bundle_missing_item import BundleMissingItem
from typing import Optional, Set
from typing_extensions import Self

class Bundle(BaseModel):
    """
    Determines how the bundle conditions are met by the customer's order items. The items in the order meet the bundle condition in the following way: SKU, then product, then collection.
    """ # noqa: E501
    quantity: Optional[Annotated[int, Field(le=100, strict=True, ge=0)]] = Field(default=None, description="Determines how many bundles are qualified. If there are missing bundle products, the value is `0`. If the bundle is qualified, the value is `1`. The maximum number of identified bundles can equal the number set in `limit`. Also defines the multiplier of the discount for `AMOUNT`, `PERCENT`, and `UNIT` discount types. To inform end-customers that more products can be added to meet additional bundles, compare this parameter with `limit`.")
    limit: Optional[Annotated[int, Field(le=100, strict=True, ge=1)]] = Field(default=None, description="Determines the maximum number of identified bundles. This also defines the maximum multiplier of the bundle discount.")
    identified: Optional[List[BundleIdentifiedItem]] = Field(default=None, description="Determines products from the customer's order items that meet bundle conditions. SKUs meet the conditions for their product that is used in the bundle. Returns only the products and their quantity that meet the bundle.")
    missing: Optional[List[BundleMissingItem]] = Field(default=None, description="Determines products, SKUs, or collections from the bundle that are missing in the customer's order items. Determines also the missing quantity. For collections, this means that order items do not include a sufficient number of items that belong to the collection. Not returned when all required bundle items are in the order.")
    __properties: ClassVar[List[str]] = ["quantity", "limit", "identified", "missing"]

    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 Bundle 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 identified (list)
        _items = []
        if self.identified:
            for _item_identified in self.identified:
                if _item_identified:
                    _items.append(_item_identified.to_dict())
            _dict['identified'] = _items
        # override the default output from pydantic by calling `to_dict()` of each item in missing (list)
        _items = []
        if self.missing:
            for _item_missing in self.missing:
                if _item_missing:
                    _items.append(_item_missing.to_dict())
            _dict['missing'] = _items
        # set to None if quantity (nullable) is None
        # and model_fields_set contains the field
        if self.quantity is None and "quantity" in self.model_fields_set:
            _dict['quantity'] = None

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "quantity": obj.get("quantity"),
            "limit": obj.get("limit"),
            "identified": [BundleIdentifiedItem.from_dict(_item) for _item in obj["identified"]] if obj.get("identified") is not None else None,
            "missing": [BundleMissingItem.from_dict(_item) for _item in obj["missing"]] if obj.get("missing") is not None else None
        })
        return _obj


