from pathlib import Path

import yaml

from app.domain.contas.workflow_actions import build_contas_workflow_actions


def test_invoice_explanation_sim_final_node_declares_final_response():
    spec = yaml.safe_load(Path("workflows/invoice_explanation.v2.yaml").read_text(encoding="utf-8"))
    nodes = {node["id"]: node for node in spec["nodes"]}
    cfg = nodes["registrar_protocolo_aceite"]["input"]
    assert cfg["workflow_response_final"] is True
    assert "{protocol}" in cfg["mensagem_final"]


def test_protocol_action_can_materialize_workflow_final_response(monkeypatch):
    class Client:
        def abrir_protocolo(self, payload):
            return {"interactionProtocol": "1234567890", "status": "OPENED"}

    class Service:
        client = Client()

    registry = build_contas_workflow_actions(Service())
    action = registry.get("registrar_protocolo_inicio")
    result = action(
        {
            "msisdn": "11999999999",
            "scenario": "invoice_explanation_aceite_fechado",
            "request_status": "Fechado",
            "status": "CLOSED",
            "workflow_response_final": True,
            "mensagem_final": "Seu número de protocolo é {protocol}.",
        },
        {"input": {}},
    )
    assert result["workflow_response_final"] is True
    assert result["protocol_number"]
    assert result["mensagem"] == f"Seu número de protocolo é {result['protocol_number']}."
