from app.domain.contas.workflow_actions import build_contas_workflow_actions


def codes(result):
    return [x.get("code") for x in result.get("business_events", []) if isinstance(x, dict)]


def test_contestacao_emite_vaa005_006_008_quando_barcode_elegivel():
    class Client:
        def billing_analysis(self, msisdn):
            return {"currentInvoice": [{"desc": "Serviços de valor adicionado", "items": [{"desc": "VAS Musica", "value": 29.9, "classe": "avulso"}]}]}
        def contestar(self, payload): return {"barcode": "123", "itemsResponse": []}
    class S: client=Client()
    action=build_contas_workflow_actions(S()).get("abrir_contestacao_cliente")  # type: ignore[arg-type]
    result=action({"msisdn":"119", "protocolo_id":"P1", "servico":"VAS Musica", "valor":29.9}, {"input":{}})
    assert result["success"] is True
    assert codes(result)[:3] == ["VAA.005", "VAA.006", "VAA.008"]


def test_contestacao_emite_vaa005_007_quando_provider_falha():
    class Client:
        def billing_analysis(self, msisdn):
            return {"currentInvoice": [{"desc": "Serviços de valor adicionado", "items": [{"desc": "VAS Musica", "value": 29.9, "classe": "avulso"}]}]}
        def contestar(self, payload): raise RuntimeError("falha provider")
    class S: client=Client()
    action=build_contas_workflow_actions(S()).get("abrir_contestacao_cliente")  # type: ignore[arg-type]
    result=action({"msisdn":"119", "protocolo_id":"P1", "servico":"VAS Musica", "valor":29.9}, {"input":{}})
    assert result["success"] is False
    assert codes(result) == ["VAA.005", "VAA.007"]


def test_sms_emite_vaa012_014_e_falha_vaa013_015():
    class Good:
        def enviar_sms(self, **kwargs): return {"resource_url":"u"}
    good=build_contas_workflow_actions(Good()).get("enviar_sms")  # type: ignore[arg-type]
    result=good({"msisdn":"119", "message":"x"}, {"input":{}})
    assert codes(result)[:2] == ["VAA.012", "VAA.014"]
    class Bad:
        def enviar_sms(self, **kwargs): raise RuntimeError("timeout")
    bad=build_contas_workflow_actions(Bad()).get("enviar_sms")  # type: ignore[arg-type]
    result=bad({"msisdn":"119", "message":"x"}, {"input":{}})
    assert codes(result) == ["VAA.013", "VAA.015"]
    assert result["sms_not_send_error"] is True


def test_status_sr_emite_vaa016_ou_017_sem_quebrar_workflow():
    class GoodClient:
        def status_sr(self, payload): return {"ok": True}
    class Good: client=GoodClient()
    action=build_contas_workflow_actions(Good()).get("atualizar_status_sr")  # type: ignore[arg-type]
    result=action({"msisdn":"119", "protocolo_id":"P1"}, {"input":{}})
    assert codes(result) == ["VAA.016"] and result["protocol_closed"] is True
    class BadClient:
        def status_sr(self, payload): raise RuntimeError("timeout")
    class Bad: client=BadClient()
    action=build_contas_workflow_actions(Bad()).get("atualizar_status_sr")  # type: ignore[arg-type]
    result=action({"msisdn":"119", "protocolo_id":"P1"}, {"input":{}})
    assert codes(result) == ["VAA.017"] and result["protocol_closed"] is False
