# coding: utf-8

"""
    ibl-data-manager

    API for iblai

    The version of the OpenAPI document: 4.202.1-ai-plus
    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 uuid import UUID
from iblai.models.item_price import ItemPrice
from iblai.models.item_type40f_enum import ItemType40fEnum
from iblai.models.status0e3_enum import Status0e3Enum
from typing import Optional, Set
from typing_extensions import Self

class ItemSubscription(BaseModel):
    """
    ItemSubscription
    """ # noqa: E501
    unique_id: UUID
    user_id: StrictInt
    username: StrictStr
    email: StrictStr
    item_type: ItemType40fEnum = Field(description="Type of item being subscribed to  * `mentor` - Mentor * `course` - Course * `program` - Program * `pathway` - Pathway")
    item_id: Annotated[str, Field(strict=True, max_length=255)] = Field(description="Identifier for the item")
    item_name: StrictStr
    status: Optional[Status0e3Enum] = Field(default=None, description="Current subscription status  * `active` - Active * `free` - Free Tier * `grandfathered` - Grandfathered * `trialing` - Trialing * `past_due` - Past Due * `canceled` - Canceled * `incomplete` - Incomplete")
    price: ItemPrice
    current_period_start: Optional[datetime] = Field(default=None, description="Start of current billing period")
    current_period_end: Optional[datetime] = Field(default=None, description="End of current billing period")
    trial_end: Optional[datetime] = Field(default=None, description="When the trial ends (if applicable)")
    cancel_at_period_end: Optional[StrictBool] = Field(default=None, description="Whether subscription will cancel at period end")
    canceled_at: Optional[datetime] = Field(default=None, description="When the subscription was canceled")
    is_grandfathered: Optional[StrictBool] = Field(default=None, description="Whether this is a grandfathered pre-paywall user")
    grandfathered_at: Optional[datetime] = Field(default=None, description="When the user was grandfathered")
    billing_portal_url: Optional[StrictStr]
    metadata: Optional[Any] = Field(default=None, description="Additional subscription metadata")
    created_at: datetime
    updated_at: datetime
    __properties: ClassVar[List[str]] = ["unique_id", "user_id", "username", "email", "item_type", "item_id", "item_name", "status", "price", "current_period_start", "current_period_end", "trial_end", "cancel_at_period_end", "canceled_at", "is_grandfathered", "grandfathered_at", "billing_portal_url", "metadata", "created_at", "updated_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 ItemSubscription 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.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        * OpenAPI `readOnly` fields are excluded.
        """
        excluded_fields: Set[str] = set([
            "unique_id",
            "user_id",
            "username",
            "email",
            "item_name",
            "price",
            "billing_portal_url",
            "created_at",
            "updated_at",
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        # override the default output from pydantic by calling `to_dict()` of price
        if self.price:
            _dict['price'] = self.price.to_dict()
        # set to None if current_period_start (nullable) is None
        # and model_fields_set contains the field
        if self.current_period_start is None and "current_period_start" in self.model_fields_set:
            _dict['current_period_start'] = None

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

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "unique_id": obj.get("unique_id"),
            "user_id": obj.get("user_id"),
            "username": obj.get("username"),
            "email": obj.get("email"),
            "item_type": obj.get("item_type"),
            "item_id": obj.get("item_id"),
            "item_name": obj.get("item_name"),
            "status": obj.get("status"),
            "price": ItemPrice.from_dict(obj["price"]) if obj.get("price") is not None else None,
            "current_period_start": obj.get("current_period_start"),
            "current_period_end": obj.get("current_period_end"),
            "trial_end": obj.get("trial_end"),
            "cancel_at_period_end": obj.get("cancel_at_period_end"),
            "canceled_at": obj.get("canceled_at"),
            "is_grandfathered": obj.get("is_grandfathered"),
            "grandfathered_at": obj.get("grandfathered_at"),
            "billing_portal_url": obj.get("billing_portal_url"),
            "metadata": obj.get("metadata"),
            "created_at": obj.get("created_at"),
            "updated_at": obj.get("updated_at")
        })
        return _obj


