"""The one figure reader. This is where the table of cases lives: every
consumer — the export's numeric column, the benchmark's scoring — calls
`as_number`, so the cases that ever went wrong are pinned here."""

from decimal import Decimal

import pytest

from quber.playground.answers.figures import as_number, is_percent, norm_printed


@pytest.mark.parametrize(
    ("printed", "expected"),
    [
        # Accounting parentheses are negatives.
        ("(21,914)", Decimal("-21914")),
        ("(0.02)", Decimal("-0.02")),
        # A currency sign preceding the parentheses must not hide them — the
        # sign-flipping defect this module exists to make unrepeatable.
        ("$ (10,550)", Decimal("-10550")),
        ("$(10,550)", Decimal("-10550")),
        # Separators and currency come off.
        ("$14.47", Decimal("14.47")),
        ("1,234,567", Decimal("1234567")),
        # A percent keeps the magnitude it was printed with, never the rate.
        ("92.6%", Decimal("92.6")),
        ("6.7 %", Decimal("6.7")),
        ("65%", Decimal("65")),
        # Multiples and scale words keep only their digits.
        ("4.6x", Decimal("4.6")),
        ("$1.2 million", Decimal("1.2")),
        ("$241.4M", Decimal("241.4")),
        ("3.1bn", Decimal("3.1")),
        # A plain negative stays negative.
        ("-4.5%", Decimal("-4.5")),
    ],
)
def test_as_number(printed, expected):
    assert as_number(printed) == expected


@pytest.mark.parametrize("printed", ["", None, "n/a", "—", "not disclosed", "USD", "1-2"])
def test_non_numeric_returns_none_rather_than_guessing(printed):
    assert as_number(printed) is None


def test_norm_printed_collapses_whitespace():
    assert norm_printed("  $ (10,550)\n") == "$ (10,550)"
    assert norm_printed(None) == ""


@pytest.mark.parametrize(
    ("printed", "expected"),
    [("6.7 %", True), ("92.6%", True), ("6.7", False), ("", False), (None, False)],
)
def test_is_percent_reads_notation_not_magnitude(printed, expected):
    assert is_percent(printed) is expected
