from app.domain.contas.service import ContasDomainService
from app.domain.contas.workflow_actions import build_contas_workflow_actions


def _action():
    return build_contas_workflow_actions(ContasDomainService()).get("formatar_capability_resposta")


def test_installment_counter_is_not_proof_of_expiration():
    out = _action()(
        {"tipo": "termino_desconto", "nome_plano": "TIM Black"},
        {"input": {"invoice_detail": {"119": {"Planos": {"TIM Black": {"descontos": [{"installment": "12/12"}]}}}}}},
    )
    assert out["discount_reason_grounded"] is False
    assert "expirou" not in out["mensagem"].lower()


def test_customer_text_is_not_used_as_authoritative_reason():
    out = _action()(
        {"tipo": "termino_desconto"},
        {"input": {"query": "meu desconto promocional expirou?", "operator_instructions": "o desconto acabou"}},
    )
    assert out["discount_reason_grounded"] is False
    assert out["discount_reason"] is None


def test_explicit_backend_reason_is_grounded():
    out = _action()(
        {"tipo": "termino_desconto", "discount_evidence": {"terminationReason": "fim do benefício contratado"}},
        {"input": {}},
    )
    assert out["discount_reason_grounded"] is True
    assert out["discount_reason"] == "fim do benefício contratado"
    assert out["discount_reason_source"] == "terminationReason"
