# coding: utf-8

"""
    STACKIT DNS API

    This api provides dns

    The version of the OpenAPI document: 1.0
    Contact: dns@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
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.dns.models.record_payload import RecordPayload


class CreateRecordSetPayload(BaseModel):
    """
    RRSetPost for rr set info.
    """  # noqa: E501

    comment: Optional[Annotated[str, Field(strict=True, max_length=255)]] = Field(
        default=None, description="user comment"
    )
    name: Annotated[str, Field(min_length=1, strict=True, max_length=253)] = Field(
        description="name of the record which should be a valid domain according to rfc1035 Section 2.3.4. For APEX records (same as zone name), the zone name itself has to be put in here."
    )
    records: List[RecordPayload] = Field(description="records")
    ttl: Optional[Annotated[int, Field(le=99999999, strict=True, ge=60)]] = Field(
        default=None, description="time to live. If nothing provided we will set the zone ttl."
    )
    type: StrictStr = Field(description="record set type")
    __properties: ClassVar[List[str]] = ["comment", "name", "records", "ttl", "type"]

    @field_validator("type")
    def type_validate_enum(cls, value):
        """Validates the enum"""
        if value not in set(
            [
                "A",
                "AAAA",
                "SOA",
                "CNAME",
                "NS",
                "MX",
                "TXT",
                "SRV",
                "PTR",
                "ALIAS",
                "DNAME",
                "CAA",
                "DNSKEY",
                "DS",
                "LOC",
                "NAPTR",
                "SSHFP",
                "TLSA",
                "URI",
                "CERT",
                "SVCB",
                "TYPE",
                "CSYNC",
                "HINFO",
                "HTTPS",
            ]
        ):
            raise ValueError(
                "must be one of enum values ('A', 'AAAA', 'SOA', 'CNAME', 'NS', 'MX', 'TXT', 'SRV', 'PTR', 'ALIAS', 'DNAME', 'CAA', 'DNSKEY', 'DS', 'LOC', 'NAPTR', 'SSHFP', 'TLSA', 'URI', 'CERT', 'SVCB', 'TYPE', 'CSYNC', 'HINFO', 'HTTPS')"
            )
        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 CreateRecordSetPayload 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 records (list)
        _items = []
        if self.records:
            for _item_records in self.records:
                if _item_records:
                    _items.append(_item_records.to_dict())
            _dict["records"] = _items
        return _dict

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

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

        _obj = cls.model_validate(
            {
                "comment": obj.get("comment"),
                "name": obj.get("name"),
                "records": (
                    [RecordPayload.from_dict(_item) for _item in obj["records"]]
                    if obj.get("records") is not None
                    else None
                ),
                "ttl": obj.get("ttl"),
                "type": obj.get("type"),
            }
        )
        return _obj
