import json
from typing import Any, Literal

from pydantic import BaseModel


class Operation(BaseModel):
    """Represents an atomic action to be performed."""

    type: Literal["click", "open_app", "scroll", "type", "wait", "navigate_back", "navigate_home"]
    args: dict[str, Any]

    @classmethod
    def from_json_android_control(cls, json_str: str) -> "Operation":
        """Create an Operation from a JSON string."""
        dict_str = json.loads(json_str)
        return cls(type=dict_str.pop("action_type"), args=dict_str)

    def to_string(self) -> str:
        """Convert the operation to a human-readable string, for instance `click(254,753)`."""
        return self.type.lower() + "(" + ",".join([repr(v).replace("'", '"') for v in self.args.values()]) + ")"


class Action(BaseModel):
    """Represents an action to be performed, which is a code representation of successive operations."""

    operations: list[Operation]

    def to_string(self) -> str:
        """Convert the action to a human-readable string, for instance `click(254,753); type("hello")`."""
        return "; ".join([operation.to_string() for operation in self.operations])
