# coding: utf-8

"""
    Mentortools Access Server

    Mentortools service for Access Packages with lazy access resolution

    The version of the OpenAPI document: 0.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, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from access_server_connector_v2.models.access_mode import AccessMode
from access_server_connector_v2.models.binding import Binding
from typing import Optional, Set
from typing_extensions import Self
from pydantic_core import to_jsonable_python

class RuleBody(BaseModel):
    """
    See resolver docstring: dynamics + envelope + window + binding, all optional.  unlock_at  = next_fire(align_cron, max(granted_at + delay_ms, starts_at?)) expires_at = min(unlock_at + access_duration_ms?, ends_at?, subscription deadline?) available  = unlock_at <= now < expires_at AND inside window AND binding grants
    """ # noqa: E501
    access_mode: Optional[AccessMode] = None
    unlock_delay_ms: Optional[Annotated[int, Field(strict=True, ge=0)]] = 0
    unlock_align_cron: Optional[StrictStr] = None
    access_duration_ms: Optional[Annotated[int, Field(strict=True, gt=0)]] = None
    starts_at: Optional[StrictInt] = None
    ends_at: Optional[StrictInt] = None
    window_cron: Optional[StrictStr] = None
    window_duration_ms: Optional[Annotated[int, Field(strict=True, gt=0)]] = None
    timezone: Optional[StrictStr] = None
    binding: Optional[Binding] = None
    __properties: ClassVar[List[str]] = ["access_mode", "unlock_delay_ms", "unlock_align_cron", "access_duration_ms", "starts_at", "ends_at", "window_cron", "window_duration_ms", "timezone", "binding"]

    model_config = ConfigDict(
        validate_by_name=True,
        validate_by_alias=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"""
        return json.dumps(to_jsonable_python(self.to_dict()))

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of RuleBody 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,
        )
        # set to None if unlock_align_cron (nullable) is None
        # and model_fields_set contains the field
        if self.unlock_align_cron is None and "unlock_align_cron" in self.model_fields_set:
            _dict['unlock_align_cron'] = None

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

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "access_mode": obj.get("access_mode"),
            "unlock_delay_ms": obj.get("unlock_delay_ms") if obj.get("unlock_delay_ms") is not None else 0,
            "unlock_align_cron": obj.get("unlock_align_cron"),
            "access_duration_ms": obj.get("access_duration_ms"),
            "starts_at": obj.get("starts_at"),
            "ends_at": obj.get("ends_at"),
            "window_cron": obj.get("window_cron"),
            "window_duration_ms": obj.get("window_duration_ms"),
            "timezone": obj.get("timezone"),
            "binding": obj.get("binding")
        })
        return _obj


