# coding: utf-8

"""
    Artifact Keeper API

    REST API for Artifact Keeper, an open-source universal artifact registry. Covers user management, repositories, artifacts, search, security scanning, SBOM, dependency tracking, SSO, analytics, monitoring, telemetry, lifecycle policies, plugins, edge nodes, migration, and administration. Format-specific protocol endpoints (npm, PyPI, Maven, Docker/OCI, etc.) are not included; those follow their respective upstream specifications.

    The version of the OpenAPI document: 1.0.0-a2
    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 datetime import datetime
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictInt, StrictStr
from typing import Any, ClassVar, Dict, List, Optional
from typing import Optional, Set
from typing_extensions import Self

class LifecyclePolicy(BaseModel):
    """
    An automated policy governing artifact retention and cleanup.
    """ # noqa: E501
    id: StrictStr = Field(description="Unique identifier for the lifecycle policy.")
    repository_id: Optional[StrictStr] = Field(default=None, description="Repository this policy is scoped to. Null indicates a global policy.")
    name: StrictStr = Field(description="Human-readable name for the policy.")
    description: Optional[StrictStr] = Field(default=None, description="Longer description of the policy purpose and behavior.")
    enabled: StrictBool = Field(description="Whether the policy is active for scheduled execution.")
    policy_type: StrictStr = Field(description="Type of lifecycle policy (e.g. retention, cleanup, archive).")
    config: Dict[str, Any] = Field(description="Policy-type-specific configuration object.")
    priority: StrictInt = Field(description="Execution priority. Lower values execute first.")
    last_run_at: Optional[datetime] = Field(default=None, description="Timestamp of the most recent policy execution.")
    last_run_items_removed: Optional[StrictInt] = Field(default=None, description="Number of artifacts removed during the most recent execution.")
    created_at: datetime = Field(description="Timestamp when the policy was created.")
    updated_at: datetime = Field(description="Timestamp when the policy was last modified.")
    __properties: ClassVar[List[str]] = ["id", "repository_id", "name", "description", "enabled", "policy_type", "config", "priority", "last_run_at", "last_run_items_removed", "created_at", "updated_at"]

    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 LifecyclePolicy 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,
        )
        # set to None if repository_id (nullable) is None
        # and model_fields_set contains the field
        if self.repository_id is None and "repository_id" in self.model_fields_set:
            _dict['repository_id'] = None

        # 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

        # set to None if last_run_at (nullable) is None
        # and model_fields_set contains the field
        if self.last_run_at is None and "last_run_at" in self.model_fields_set:
            _dict['last_run_at'] = None

        # set to None if last_run_items_removed (nullable) is None
        # and model_fields_set contains the field
        if self.last_run_items_removed is None and "last_run_items_removed" in self.model_fields_set:
            _dict['last_run_items_removed'] = None

        return _dict

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

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

        _obj = cls.model_validate({
            "id": obj.get("id"),
            "repository_id": obj.get("repository_id"),
            "name": obj.get("name"),
            "description": obj.get("description"),
            "enabled": obj.get("enabled"),
            "policy_type": obj.get("policy_type"),
            "config": obj.get("config"),
            "priority": obj.get("priority"),
            "last_run_at": obj.get("last_run_at"),
            "last_run_items_removed": obj.get("last_run_items_removed"),
            "created_at": obj.get("created_at"),
            "updated_at": obj.get("updated_at")
        })
        return _obj


