# coding: utf-8

"""
    Ringer Business API - Packages Management

    This API provides endpoints for managing packages. Packages are collections of items or features that can be included in or added to subscription plans. This API allows retrieving details of specific packages and listing available packages.  - **Included Packages**: Provide a quantity of a specific rate item which is included in the plan subscription cost. - **Optional Packages**: Provide a quantity of a specific rate item for an additional monthly fee. 

    The version of the OpenAPI document: 1.0.0
    Contact: support@ringer.tel
    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, StrictInt, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from ringer_business.models.package_feature import PackageFeature
from typing import Optional, Set
from typing_extensions import Self

class PackageCore(BaseModel):
    """
    PackageCore
    """ # noqa: E501
    id: StrictInt = Field(description="Unique identifier for the package.")
    name: StrictStr = Field(description="The name of the package.")
    description: Optional[StrictStr] = Field(default=None, description="A brief description of what the package includes or its purpose.")
    item_type: StrictStr = Field(description="The type of item this package represents or groups (e.g., subscription, feature_bundle, usage_minutes).")
    item_rules: Optional[StrictStr] = Field(default=None, description="Specific rules or subclassifications for the item type.")
    item_quantity: StrictInt = Field(description="The quantity of the item provided by this package (e.g., number of minutes, 1 for a subscription).")
    price: StrictStr = Field(description="The cost of this package if it's optional or its contribution to a plan's price.")
    currency: StrictStr = Field(description="Three-letter ISO currency code for the package price.")
    plan_id: Optional[StrictInt] = Field(default=None, description="Identifier of the plan this package definition might be primarily associated with or templated from.")
    included: StrictBool = Field(description="True if this package is an included part of a plan, false if it's optional.")
    removable: StrictBool = Field(description="Indicates if this package can be removed from a plan by the customer.")
    category: Optional[StrictStr] = Field(default=None, description="Category of the package")
    renewable: Optional[StrictBool] = Field(default=None, description="Whether the package renews automatically")
    features: Optional[List[PackageFeature]] = Field(default=None, description="List of features included in the package")
    __properties: ClassVar[List[str]] = ["id", "name", "description", "item_type", "item_rules", "item_quantity", "price", "currency", "plan_id", "included", "removable", "category", "renewable", "features"]

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

        if value not in set(['calling', 'messaging', 'conferencing', 'faxing', 'security', 'base_plan', 'add_on']):
            raise ValueError("must be one of enum values ('calling', 'messaging', 'conferencing', 'faxing', 'security', 'base_plan', 'add_on')")
        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"""
        # 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 PackageCore 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 features (list)
        _items = []
        if self.features:
            for _item_features in self.features:
                if _item_features:
                    _items.append(_item_features.to_dict())
            _dict['features'] = _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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "id": obj.get("id"),
            "name": obj.get("name"),
            "description": obj.get("description"),
            "item_type": obj.get("item_type"),
            "item_rules": obj.get("item_rules"),
            "item_quantity": obj.get("item_quantity"),
            "price": obj.get("price"),
            "currency": obj.get("currency") if obj.get("currency") is not None else 'USD',
            "plan_id": obj.get("plan_id"),
            "included": obj.get("included"),
            "removable": obj.get("removable"),
            "category": obj.get("category"),
            "renewable": obj.get("renewable"),
            "features": [PackageFeature.from_dict(_item) for _item in obj["features"]] if obj.get("features") is not None else None
        })
        return _obj


