#!/usr/bin/env python3
import json
from pathlib import Path


def execute_static_plan():
    state = {"document_available": True, "copied": False, "sent": False}
    plan = ["copy_document", "send_document"]
    trace = []
    for index, action in enumerate(plan):
        if index == 0:
            state["document_available"] = False  # 环境在计划生成后发生变化
        if action == "copy_document" and not state["document_available"]:
            trace.append({"action": action, "status": "failed", "reason": "document_moved"})
            return {"terminal": "failed", "trace": trace}
    return {"terminal": "success", "trace": trace}


def execute_rolling_plan():
    state = {"document_available": True, "backup_available": True, "copied": False, "sent": False}
    trace = []
    state["document_available"] = False
    if not state["document_available"] and state["backup_available"]:
        trace.append({"decision": "replan", "reason": "document_moved", "next": "copy_backup"})
        state["copied"] = True
        trace.append({"action": "copy_backup", "status": "success"})
    if state["copied"]:
        state["sent"] = True
        trace.append({"action": "send_document", "status": "success"})
    return {"terminal": "success" if state["sent"] else "failed", "trace": trace}


result = {"static_plan": execute_static_plan(), "rolling_plan": execute_rolling_plan()}
Path(__file__).with_name("experiment-result.json").write_text(
    json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
)
print(json.dumps(result, ensure_ascii=False, indent=2))
