# coding: utf-8

"""
    STACKIT Marketplace API

    API to manage STACKIT Marketplace.

    The version of the OpenAPI document: 1
    Contact: marketplace@stackit.cloud
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501

from __future__ import annotations

import json
import pprint
import re  # noqa: F401
from typing import Any, ClassVar, Dict, List, Optional, Set

from pydantic import BaseModel, ConfigDict, Field, StrictStr, field_validator
from typing_extensions import Annotated, Self

from stackit.stackitmarketplace.models.assets import Assets
from stackit.stackitmarketplace.models.delivery_method import DeliveryMethod
from stackit.stackitmarketplace.models.price_type import PriceType
from stackit.stackitmarketplace.models.product_lifecycle_state import (
    ProductLifecycleState,
)


class SubscriptionProduct(BaseModel):
    """
    The product of a subscription
    """  # noqa: E501

    assets: Optional[Assets] = None
    delivery_method: DeliveryMethod = Field(alias="deliveryMethod")
    lifecycle_state: ProductLifecycleState = Field(alias="lifecycleState")
    plan_id: Annotated[str, Field(min_length=10, strict=True, max_length=29)] = Field(
        description="The user-readable plan ID of a pricing option.", alias="planId"
    )
    price_type: PriceType = Field(alias="priceType")
    pricing_plan: StrictStr = Field(description="Additional price type information.", alias="pricingPlan")
    product_id: Annotated[str, Field(min_length=10, strict=True, max_length=29)] = Field(
        description="The user-readable product ID.", alias="productId"
    )
    product_name: Annotated[str, Field(strict=True, max_length=512)] = Field(
        description="The name of the product.", alias="productName"
    )
    vendor_name: Annotated[str, Field(strict=True, max_length=512)] = Field(
        description="The product's vendor name.", alias="vendorName"
    )
    vendor_plan_id: Optional[Annotated[str, Field(strict=True)]] = Field(
        default=None, description="The vendor provided plan ID.", alias="vendorPlanId"
    )
    vendor_product_id: Optional[Annotated[str, Field(strict=True)]] = Field(
        default=None, description="The vendor provided product ID.", alias="vendorProductId"
    )
    vendor_website_url: Annotated[str, Field(strict=True, max_length=512)] = Field(
        description="The vendor's website.", alias="vendorWebsiteUrl"
    )
    __properties: ClassVar[List[str]] = [
        "assets",
        "deliveryMethod",
        "lifecycleState",
        "planId",
        "priceType",
        "pricingPlan",
        "productId",
        "productName",
        "vendorName",
        "vendorPlanId",
        "vendorProductId",
        "vendorWebsiteUrl",
    ]

    @field_validator("plan_id")
    def plan_id_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^[a-z0-9-]{1,20}-[0-9a-f]{8}$", value):
            raise ValueError(r"must validate the regular expression /^[a-z0-9-]{1,20}-[0-9a-f]{8}$/")
        return value

    @field_validator("product_id")
    def product_id_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^[a-z0-9-]{1,20}-[0-9a-f]{8}$", value):
            raise ValueError(r"must validate the regular expression /^[a-z0-9-]{1,20}-[0-9a-f]{8}$/")
        return value

    @field_validator("product_name")
    def product_name_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^[a-zA-ZäüöÄÜÖ0-9,.!?()@\/:=\n\t -]+$", value):
            raise ValueError(r"must validate the regular expression /^[a-zA-ZäüöÄÜÖ0-9,.!?()@\/:=\n\t -]+$/")
        return value

    @field_validator("vendor_name")
    def vendor_name_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^[a-zA-ZäüöÄÜÖ0-9,.!?()@\/:=\n\t -]+$", value):
            raise ValueError(r"must validate the regular expression /^[a-zA-ZäüöÄÜÖ0-9,.!?()@\/:=\n\t -]+$/")
        return value

    @field_validator("vendor_plan_id")
    def vendor_plan_id_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"^[a-zA-Z0-9](?:[a-zA-Z0-9_+&-]){0,39}$", value):
            raise ValueError(r"must validate the regular expression /^[a-zA-Z0-9](?:[a-zA-Z0-9_+&-]){0,39}$/")
        return value

    @field_validator("vendor_product_id")
    def vendor_product_id_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"^[a-zA-Z0-9](?:[a-zA-Z0-9_+&-]){0,39}$", value):
            raise ValueError(r"must validate the regular expression /^[a-zA-Z0-9](?:[a-zA-Z0-9_+&-]){0,39}$/")
        return value

    @field_validator("vendor_website_url")
    def vendor_website_url_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$", value):
            raise ValueError(
                r"must validate the regular expression /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/"
            )
        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 SubscriptionProduct 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 assets
        if self.assets:
            _dict["assets"] = self.assets.to_dict()
        return _dict

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

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

        _obj = cls.model_validate(
            {
                "assets": Assets.from_dict(obj["assets"]) if obj.get("assets") is not None else None,
                "deliveryMethod": obj.get("deliveryMethod"),
                "lifecycleState": obj.get("lifecycleState"),
                "planId": obj.get("planId"),
                "priceType": obj.get("priceType"),
                "pricingPlan": obj.get("pricingPlan"),
                "productId": obj.get("productId"),
                "productName": obj.get("productName"),
                "vendorName": obj.get("vendorName"),
                "vendorPlanId": obj.get("vendorPlanId"),
                "vendorProductId": obj.get("vendorProductId"),
                "vendorWebsiteUrl": obj.get("vendorWebsiteUrl"),
            }
        )
        return _obj
