# 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, StrictFloat, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional, Union
from typing import Optional, Set
from typing_extensions import Self

class CodeConfig(BaseModel):
    """
    Contains information about the config used for the voucher code. Defines the code's pattern (prefix, postfix, length, charset, etc).
    """ # noqa: E501
    length: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Number of characters in a generated code (excluding prefix and postfix).")
    charset: Optional[StrictStr] = Field(default=None, description="Characters that can appear in the code.    Examples:  - Alphanumeric: `0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`  - Alphabetic: `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`  - Alphabetic Lowercase: `abcdefghijklmnopqrstuvwxyz`  - Alphabetic Uppercase: `ABCDEFGHIJKLMNOPQRSTUVWXYZ`  - Numbers: `0123456789`   - Custom: a custom character set")
    prefix: Optional[StrictStr] = Field(default=None, description="A text appended before the code.")
    postfix: Optional[StrictStr] = Field(default=None, description="A text appended after the code.")
    pattern: Optional[StrictStr] = Field(default=None, description="A pattern for codes where hashes (#) will be replaced with random characters. Overrides `length`.")
    initial_count: Optional[StrictInt] = Field(default=None, description="Internal value, does not change anything if provided.")
    __properties: ClassVar[List[str]] = ["length", "charset", "prefix", "postfix", "pattern", "initial_count"]

    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 CodeConfig 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 length (nullable) is None
        # and model_fields_set contains the field
        if self.length is None and "length" in self.model_fields_set:
            _dict['length'] = None

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

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

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

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

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

        return _dict

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

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

        _obj = cls.model_validate({
            "length": obj.get("length"),
            "charset": obj.get("charset"),
            "prefix": obj.get("prefix"),
            "postfix": obj.get("postfix"),
            "pattern": obj.get("pattern"),
            "initial_count": obj.get("initial_count")
        })
        return _obj


