"""Exact offline arithmetic, not a trading integration or live fee schedule.
Run: python3 polymarket-negative-risk.py
All prices, sizes and costs are fictional. No network or wallet calls.
"""
from fractions import Fraction as F


def valid_event(names, winner):
    if len(names) < 2 or len(set(names)) != len(names) or winner not in names:
        raise ValueError('unique complete event and exactly one listed winner required')


def payoff_check(names, excluded, winner, quantity):
    valid_event(names, winner)
    if excluded not in names or quantity < 0:
        raise ValueError('listed excluded outcome and nonnegative quantity required')
    original = quantity * (winner != excluded)
    converted = sum(quantity * (winner == name) for name in names if name != excluded)
    return original, converted


def closed_difference(quantity, entry, bids, total_cost):
    if quantity < 0 or not 0 <= entry <= 1 or total_cost < 0 or not bids or any(not 0 <= p <= 1 for p in bids):
        raise ValueError('nonnegative size/cost and unit prices in0..1 required')
    return quantity * (sum(bids) - entry) - total_cost


def examples():
    names = ['A', 'B', 'C']
    for winner in names:
        before, after = payoff_check(names, 'A', winner, F(100))
        assert before == after
    # Independent, explicit three-state expected payoff vector.
    assert [payoff_check(names, 'A', w, F(100))[1] for w in names] == [0, 100, 100]
    assert closed_difference(F(100), F('.58'), [F('.36'), F('.25')], F(1)) == 2
    assert closed_difference(F(100), F('.58'), [F('.36'), F('.25')], F(3)) == 0
    assert closed_difference(F(100), F('.58'), [F('.36'), F('.25')], F(4)) == -1
    capacity = min(F(100), F(100), F(40))  # entry, B exit, C exit
    assert capacity == 40
    assert capacity * F('.58') == F('23.20')
    assert capacity * (F('.36') + F('.25')) == F('24.40')
    assert closed_difference(capacity, F('.58'), [F('.36'), F('.25')], F('.40')) == F('.80')
    # Full conversion with a partial C exit: reconcile held inventory by outcome.
    cash = F(100) * F('.36') + F(40) * F('.25')
    remaining_c = 100 - 40
    results = [cash + remaining_c * (winner == 'C') - F(100) * F('.58') - 1 for winner in names]
    assert cash == 46 and results == [-13, -13, 47]
    # Missing D breaks equivalence; it is NOT a valid three-outcome conversion.
    no_a_when_d_wins = 1
    b_plus_c_when_d_wins = 0
    assert no_a_when_d_wins != b_plus_c_when_d_wins
    # Overlapping winners also break the stipulated exactly-one structure.
    no_a_when_b_and_c_win = 1
    b_plus_c_when_b_and_c_win = 2
    assert no_a_when_b_and_c_win != b_plus_c_when_b_and_c_win
    for bad_names, winner in [(['A', 'A'], 'A'), (['A'], 'A'), (names, 'D')]:
        try:
            valid_event(bad_names, winner)
        except ValueError:
            pass
        else:
            raise AssertionError('invalid model event accepted')
    print('Complete exit100units: net2 under stipulated one-unit total cost')
    print('Complete exit40units: net0.80 under stipulated0.40 total cost')
    print('Partial C exit: results by winner A/B/C:', results)
    print('Payoff, cash-flow, cost-sensitivity and counterexample checks passed')


if __name__ == '__main__':
    examples()
