from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass
class ActionDecision:
    """表示模型给出的下一步动作决策。"""

    action: str
    args: dict[str, Any] = field(default_factory=dict)
    reason: str = ""
    output: dict[str, Any] | None = None


@dataclass
class StepLog:
    """记录一次观察-决策-执行步骤。"""

    step: int
    screenshot: str
    action: str
    args: dict[str, Any]
    reason: str
    status: str
    observation: str


@dataclass
class RunResult:
    """表示一次任务运行的最终结果。"""

    status: str
    task: str
    start_url: str
    current_url: str
    page_title: str
    screenshot: str
    model: dict[str, Any]
    output_schema: dict[str, Any] | None
    output: dict[str, Any] | None
    steps: list[StepLog]
    note: str

    def to_dict(self) -> dict[str, Any]:
        """将结果对象转换为可序列化字典。"""
        return {
            "status": self.status,
            "task": self.task,
            "start_url": self.start_url,
            "current_url": self.current_url,
            "page_title": self.page_title,
            "screenshot": self.screenshot,
            "model": self.model,
            "output_schema": self.output_schema,
            "output": self.output,
            "steps": [
                {
                    "step": s.step,
                    "screenshot": s.screenshot,
                    "action": s.action,
                    "args": s.args,
                    "reason": s.reason,
                    "status": s.status,
                    "observation": s.observation,
                }
                for s in self.steps
            ],
            "note": self.note,
        }
