from agent_framework.runtime.agent_runtime import AgentRuntimeMixin


class Runtime(AgentRuntimeMixin):
    pass


def _envelope(status: str, name: str = "invoice_explanation"):
    return {
        "result": {
            "result": {
                "status": status,
                "execution_id": "exec-1",
                "metadata": {
                    "workflow_name": name,
                    "workflow_execution_id": "exec-1",
                    "resume_tool": "retomar_workflow",
                },
                "pause": {"message": "Sanei sua dúvida?"} if status == "PAUSED" else None,
            }
        }
    }


def test_workflow_pausado_ja_e_registrado_como_executado():
    runtime = Runtime()
    state = {}
    runtime._capture_pending_domain_workflow(state, _envelope("PAUSED"))
    assert state["business_workflows_executed"] == ["invoice_explanation"]
    assert state["pending_domain_workflow"]["workflow_name"] == "invoice_explanation"


def test_workflow_completo_e_registrado_sem_pending():
    runtime = Runtime()
    state = {}
    runtime._capture_pending_domain_workflow(state, _envelope("COMPLETED", "pro_rata"))
    assert state["business_workflows_executed"] == ["pro_rata"]
    assert state["pending_domain_workflow"] is None


def test_latch_nao_duplica_workflow():
    runtime = Runtime()
    state = {}
    runtime._capture_pending_domain_workflow(state, _envelope("PAUSED"))
    runtime._capture_pending_domain_workflow(state, _envelope("COMPLETED"))
    assert state["business_workflows_executed"] == ["invoice_explanation"]


def test_transaction_state_patch_preserva_latch():
    runtime = Runtime()
    patch = runtime.transaction_state_patch({"business_workflows_executed": ["invoice_explanation"]})
    assert patch["business_workflows_executed"] == ["invoice_explanation"]


def test_transaction_state_patch_preserva_boundary_de_soft_reset():
    runtime = Runtime()
    patch = runtime.transaction_state_patch({
        "transaction_status": "COMPLETED",
        "operational_context_boundary_pending": True,
    })
    assert patch["transaction_status"] == "COMPLETED"
    assert patch["operational_context_boundary_pending"] is True


def test_workflow_response_final_fecha_latch_mesmo_se_adapter_retornar_paused():
    runtime = Runtime()
    state = {
        "pending_domain_workflow": {
            "execution_id": "exec-1",
            "workflow_name": "invoice_explanation",
        },
        "transaction_status": "WORKFLOW_PAUSED",
    }
    envelope = {
        "result": {
            "result": {
                "status": "PAUSED",
                "execution_id": "exec-1",
                "metadata": {
                    "workflow_name": "invoice_explanation",
                    "workflow_execution_id": "exec-1",
                    "resume_tool": "retomar_workflow",
                },
                "output": {
                    "workflow_response_final": True,
                    "mensagem": "Seu número de protocolo é 1234567890.",
                },
                "state": {"current_node": "registrar_protocolo_aceite"},
            }
        }
    }
    runtime._capture_pending_domain_workflow(state, envelope)
    assert state["pending_domain_workflow"] is None
    assert state["transaction_status"] == "COMPLETED"
    assert state["operational_context_boundary_pending"] is True
