# coding: utf-8

"""
FastAPI

No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

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 json
import pprint
import re  # noqa: F401
from typing import Dict, Optional

from truefoundry.ml.autogen.client.models.method import Method
from truefoundry.ml.autogen.client.models.openapi_spec import OpenapiSpec
from truefoundry.pydantic_v1 import BaseModel, Field, StrictStr, constr, validator


class AgentOpenAPITool(BaseModel):
    """
    OpenAPI Tool artifact.  # noqa: E501
    """

    type: Optional[StrictStr] = "openapi-tool"
    name: constr(strict=True) = Field(
        default=..., description="Names cannot repeat accross tools +uiType=Hidden"
    )
    description: constr(strict=True, max_length=128, min_length=1) = Field(
        default=..., description="+sort=10 +uiType=TextArea"
    )
    openapi_spec: OpenapiSpec = Field(...)
    base_url: constr(strict=True) = Field(default=..., description="+sort=30")
    path: constr(strict=True, min_length=1) = Field(
        default=...,
        description="+sort=40 +uiType=MethodPathSelector +label=Method + Path +usage=Select one Method and Path for this tool",
    )
    method: Method = Field(default=..., description="+sort=50 +uiType=Hidden")
    headers: Optional[Dict[str, StrictStr]] = Field(
        default=None,
        description='+sort=60 +uiType=KV +uiProps={"allowSecrets":true,"secretConfig":{"enableNew":true,"hideOptions":true}}',
    )
    __properties = [
        "type",
        "name",
        "description",
        "openapi_spec",
        "base_url",
        "path",
        "method",
        "headers",
    ]

    @validator("type")
    def type_validate_enum(cls, value):
        """Validates the enum"""
        if value is None:
            return value

        if value not in ("openapi-tool",):
            raise ValueError("must be one of enum values ('openapi-tool')")
        return value

    @validator("name")
    def name_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(r"^[a-zA-Z][a-zA-Z0-9\-]{1,30}[a-zA-Z0-9]$", value):
            raise ValueError(
                r"must validate the regular expression /^[a-zA-Z][a-zA-Z0-9\-]{1,30}[a-zA-Z0-9]$/"
            )
        return value

    @validator("base_url")
    def base_url_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if not re.match(
            r"^(https?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$",
            value,
        ):
            raise ValueError(
                r"must validate the regular expression /^(https?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/"
            )
        return value

    class Config:
        """Pydantic configuration"""

        allow_population_by_field_name = True
        validate_assignment = True

    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.dict(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> AgentOpenAPITool:
        """Create an instance of AgentOpenAPITool from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self):
        """Returns the dictionary representation of the model using alias"""
        _dict = self.dict(by_alias=True, exclude={}, exclude_none=True)
        # override the default output from truefoundry.pydantic_v1 by calling `to_dict()` of openapi_spec
        if self.openapi_spec:
            _dict["openapi_spec"] = self.openapi_spec.to_dict()
        return _dict

    @classmethod
    def from_dict(cls, obj: dict) -> AgentOpenAPITool:
        """Create an instance of AgentOpenAPITool from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return AgentOpenAPITool.parse_obj(obj)

        _obj = AgentOpenAPITool.parse_obj(
            {
                "type": obj.get("type")
                if obj.get("type") is not None
                else "openapi-tool",
                "name": obj.get("name"),
                "description": obj.get("description"),
                "openapi_spec": OpenapiSpec.from_dict(obj.get("openapi_spec"))
                if obj.get("openapi_spec") is not None
                else None,
                "base_url": obj.get("base_url"),
                "path": obj.get("path"),
                "method": obj.get("method"),
                "headers": obj.get("headers"),
            }
        )
        return _obj
