# coding: utf-8

"""
    bxb API

    A comprehensive usage-based billing platform API. Manage customers, plans, subscriptions, invoices, payments, wallets, coupons, credit notes, taxes, webhooks, and more.

    The version of the OpenAPI document: 0.5.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, StrictStr, field_validator
from typing import Any, ClassVar, Dict, List, Optional
from typing_extensions import Annotated
from bxb.models.dunning_campaign_threshold_create import DunningCampaignThresholdCreate
from typing import Optional, Set
from typing_extensions import Self

class DunningCampaignCreate(BaseModel):
    """
    Schema for creating a dunning campaign.
    """ # noqa: E501
    code: Annotated[str, Field(min_length=1, strict=True, max_length=255)]
    name: Annotated[str, Field(min_length=1, strict=True, max_length=255)]
    description: Optional[StrictStr] = None
    max_attempts: Optional[Annotated[int, Field(strict=True, ge=1)]] = 3
    days_between_attempts: Optional[Annotated[int, Field(strict=True, ge=1)]] = 3
    bcc_emails: Optional[List[StrictStr]] = None
    status: Optional[Annotated[str, Field(strict=True)]] = 'active'
    thresholds: Optional[List[DunningCampaignThresholdCreate]] = None
    __properties: ClassVar[List[str]] = ["code", "name", "description", "max_attempts", "days_between_attempts", "bcc_emails", "status", "thresholds"]

    @field_validator('status')
    def status_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"^(active|inactive)$", value):
            raise ValueError(r"must validate the regular expression /^(active|inactive)$/")
        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 DunningCampaignCreate 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 thresholds (list)
        _items = []
        if self.thresholds:
            for _item_thresholds in self.thresholds:
                if _item_thresholds:
                    _items.append(_item_thresholds.to_dict())
            _dict['thresholds'] = _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

        return _dict

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

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

        _obj = cls.model_validate({
            "code": obj.get("code"),
            "name": obj.get("name"),
            "description": obj.get("description"),
            "max_attempts": obj.get("max_attempts") if obj.get("max_attempts") is not None else 3,
            "days_between_attempts": obj.get("days_between_attempts") if obj.get("days_between_attempts") is not None else 3,
            "bcc_emails": obj.get("bcc_emails"),
            "status": obj.get("status") if obj.get("status") is not None else 'active',
            "thresholds": [DunningCampaignThresholdCreate.from_dict(_item) for _item in obj["thresholds"]] if obj.get("thresholds") is not None else None
        })
        return _obj


