
"""
ARL Infrastructure API

OpenAPI specification for ARL (Agentic RL) Infrastructure custom resources. This SDK provides models for WarmPool, Sandbox, and Task Kubernetes custom resources.

The version of the OpenAPI document: v1alpha1
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
"""

from __future__ import annotations

import json
import pprint
import re  # noqa: F401
from typing import Any, ClassVar

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


class TaskStep(BaseModel):
    """
    Task step definition. Each step can be either:  1. **FilePatch**: Create or modify a file    - Required: name, type=\"FilePatch\", path, content    - Example: {\"name\": \"write\", \"type\": \"FilePatch\", \"path\": \"/workspace/test.py\", \"content\": \"print('test')\"}  2. **Command**: Execute a command    - Required: name, type=\"Command\", command    - Optional: workDir, env    - Example: {\"name\": \"run\", \"type\": \"Command\", \"command\": [\"python\", \"test.py\"], \"env\": {\"DEBUG\": \"1\"}}
    """

    command: list[StrictStr] | None = Field(
        default=None,
        description="Command and arguments to execute (required for Command steps, ignored for FilePatch steps)",
    )
    content: StrictStr | None = Field(
        default=None,
        description="File content to write (required for FilePatch steps, ignored for Command steps)",
    )
    env: dict[str, StrictStr] | None = Field(
        default=None,
        description="Environment variables as key-value pairs (optional, only for Command steps)",
    )
    name: StrictStr = Field(description="Step identifier (unique within the task)")
    path: StrictStr | None = Field(
        default=None,
        description="File path to create or modify (required for FilePatch steps, ignored for Command steps)",
    )
    type: StrictStr = Field(
        description="Step type - either 'FilePatch' (create/modify files) or 'Command' (execute commands)"
    )
    work_dir: StrictStr | None = Field(
        default=None,
        description="Working directory for command execution (optional, only for Command steps)",
        alias="workDir",
    )
    __properties: ClassVar[list[str]] = [
        "command",
        "content",
        "env",
        "name",
        "path",
        "type",
        "workDir",
    ]

    @field_validator("type")
    def type_validate_enum(cls, value):
        """Validates the enum"""
        if value not in set(["FilePatch", "Command"]):
            raise ValueError("must be one of enum values ('FilePatch', 'Command')")
        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) -> Self | None:
        """Create an instance of TaskStep 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,
        )
        return _dict

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

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

        _obj = cls.model_validate(
            {
                "command": obj.get("command"),
                "content": obj.get("content"),
                "env": obj.get("env"),
                "name": obj.get("name"),
                "path": obj.get("path"),
                "type": obj.get("type"),
                "workDir": obj.get("workDir"),
            }
        )
        return _obj
