from decimal import Decimal

from app.domain.contas.client import TimApiClient
from app.domain.contas.workflow_actions import _classify_contestation_items


def test_mock_contestar_filters_provider_response_to_current_transaction(monkeypatch):
    monkeypatch.setenv("TIM_USE_MOCK_GATEWAY", "true")
    client = TimApiClient()
    result = client.contestar({
        "sr": "1234567890",
        "items": [{"itemName": "Tamboro Mensal", "claimedAmount": "14.99", "validatedAmount": "14.99"}],
    })
    assert result["sr"] == "1234567890"
    assert [x["itemName"] for x in result["itemsResponse"]] == ["Tamboro Mensal"]
    assert result["itemsResponse"][0]["message"] == "Contestação criada/atualizada com sucesso"
    assert "items_response" not in result
    assert "validated_items" not in result
    assert "protocol_number" not in result


def test_contestation_amount_does_not_convert_decimal_dot_to_cents():
    requested = [{"item_name": "Tamboro Mensal", "claimed_amount": "14.99", "validated_amount": "14.99"}]
    response = [{"itemName": "Tamboro Mensal", "status": "INICIADA", "correctAccountStatus": "CRIAR"}]
    contested, not_contested, already, open_amount, total_amount = _classify_contestation_items(requested, response)
    assert len(contested) == 1
    assert not not_contested
    assert not already
    assert Decimal(open_amount) == Decimal("14.99")
    assert Decimal(total_amount) == Decimal("14.99")
