"""Synthetic procurement model. Python 3.11+, standard library, no network.
No vendor quote or measured performance. Deterministic: no random seed.
Money and probabilities use Decimal. A billable claim is not durable resolution.
"""
from decimal import Decimal as D


def economics(n=100000, eligible='0.70', solved='0.75', repeat='0.08',
              bill='1.20', variable='0.12', human='4', fixed='25000',
              loss='5000', baseline='6'):
    if not isinstance(n, int) or isinstance(n, bool) or n < 0:
        raise ValueError('n must be a non-negative integer')
    e, s, r = map(D, (eligible, solved, repeat))
    b, c, h, f, risk, h0 = map(D, (bill, variable, human, fixed, loss, baseline))
    if any(not x.is_finite() or not 0 <= x <= 1 for x in (e, s, r)):
        raise ValueError('probabilities must be finite and in [0,1]')
    if any(not x.is_finite() or x < 0 for x in (b, c, h, f, risk, h0)):
        raise ValueError('costs must be finite and non-negative')
    claims = D(n) * e * s
    durable = claims * (1-r)
    # Residual human work includes ineligible, unsolved and repeat contacts.
    # This simplified model counts one human intervention per residual case.
    total = claims*b + D(n)*c + (D(n)-durable)*h + f + risk
    return {'claims': claims, 'durable': durable, 'cost': total,
            'savings': D(n)*h0-total}


def check():
    first = economics()
    assert first == dict(claims=D(52500), durable=D(48300),
                         cost=D(311800), savings=D(288200))
    second = economics(solved='0.60', repeat='0.15')
    assert second == dict(claims=D(42000), durable=D(35700),
                          cost=D(349600), savings=D(250400))
    assert economics(eligible='0')['durable'] == 0
    assert economics(repeat='1')['durable'] == 0
    assert economics(n=0)['cost'] == D(30000)  # fixed costs still exist
    assert first['claims'] > first['durable']
    for args in ({'eligible':'1.1'}, {'repeat':'-0.1'}, {'bill':'-1'},
                 {'fixed':'NaN'}, {'n':-1}, {'n':True}):
        try:
            economics(**args)
        except ValueError:
            pass
        else:
            raise AssertionError(args)
    for label, case in [('base',first),('stress',second)]:
        print(label, ', '.join(f'{k}={v:,.0f}' for k,v in case.items()))
    print('PASS: 2 scenarios; boundary and invalid-input checks')


if __name__ == '__main__':
    check()
