# coding: utf-8

"""
TrueFoundry ML API

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

The version of the OpenAPI document: 1.0.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 Any, Dict, Optional

from truefoundry.ml._autogen.client.models.source import Source
from truefoundry.pydantic_v1 import (
    BaseModel,
    Field,
    StrictStr,
    conint,
    constr,
    validator,
)


class ArtifactManifest(BaseModel):
    """
    Artifact Version manifest.  # noqa: E501
    """

    name: Optional[constr(strict=True, max_length=256)] = Field(
        default=None, description="Name of the entity"
    )
    description: Optional[constr(strict=True, max_length=512)] = None
    metadata: Dict[str, Any] = Field(
        default=...,
        description='Key value metadata. Should be valid JSON. For e.g. `{"business-unit": "sales", "quality": "good", "rating": 4.5}`',
    )
    version_alias: Optional[constr(strict=True, max_length=128)] = Field(
        default=None,
        description="Version alias is alternate, ideally human readable, version string to reference an artifact version. It should start with `v` followed by alphanumeric and it can include `.` and `-` in between (e.g. `v1.0.0`, `v1-prod`, `v3-dev`, etc)",
    )
    ml_repo: Optional[constr(strict=True)] = Field(
        default=None, description="Name of the ML Repo"
    )
    version: Optional[conint(strict=True, ge=1)] = Field(
        default=None, description="Version of the entity"
    )
    type: Optional[StrictStr] = "artifact-version"
    source: Source = Field(...)
    step: Optional[conint(strict=True, ge=0)] = Field(
        default=0,
        description="Step/Epoch number in an iterative training loop the artifact version was created. Generally useful when logging a model version from a MLRepo Run",
    )
    run_id: Optional[StrictStr] = Field(
        default=None,
        description="ID of the MLRepo Run that generated the artifact version",
    )
    __properties = [
        "name",
        "description",
        "metadata",
        "version_alias",
        "ml_repo",
        "version",
        "type",
        "source",
        "step",
        "run_id",
    ]

    @validator("name")
    def name_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"^[A-Za-z0-9_\-]+$", value):
            raise ValueError(
                r"must validate the regular expression /^[A-Za-z0-9_\-]+$/"
            )
        return value

    @validator("version_alias")
    def version_alias_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"^v[a-zA-Z0-9.-]*([a-zA-Z0-9]+)$", value):
            raise ValueError(
                r"must validate the regular expression /^v[a-zA-Z0-9.-]*([a-zA-Z0-9]+)$/"
            )
        return value

    @validator("ml_repo")
    def ml_repo_validate_regular_expression(cls, value):
        """Validates the regular expression"""
        if value is None:
            return value

        if not re.match(r"^[a-zA-Z][a-zA-Z0-9\-]{1,98}[a-zA-Z0-9]$", value):
            raise ValueError(
                r"must validate the regular expression /^[a-zA-Z][a-zA-Z0-9\-]{1,98}[a-zA-Z0-9]$/"
            )
        return value

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

        if value not in ("artifact-version",):
            raise ValueError("must be one of enum values ('artifact-version')")
        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) -> ArtifactManifest:
        """Create an instance of ArtifactManifest 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 source
        if self.source:
            _dict["source"] = self.source.to_dict()
        return _dict

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

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

        _obj = ArtifactManifest.parse_obj(
            {
                "name": obj.get("name"),
                "description": obj.get("description"),
                "metadata": obj.get("metadata"),
                "version_alias": obj.get("version_alias"),
                "ml_repo": obj.get("ml_repo"),
                "version": obj.get("version"),
                "type": obj.get("type")
                if obj.get("type") is not None
                else "artifact-version",
                "source": Source.from_dict(obj.get("source"))
                if obj.get("source") is not None
                else None,
                "step": obj.get("step") if obj.get("step") is not None else 0,
                "run_id": obj.get("run_id"),
            }
        )
        return _obj
