"""Four synthetic matrix fixtures. In-memory, single-process, no authorisation."""
from fractions import Fraction

def count(value):
    if isinstance(value, bool) or not isinstance(value, int) or value < 0:
        raise ValueError('A nonnegative integer count is required')
    return value

def contribution(orders, per_order):
    orders = count(orders)
    if isinstance(per_order, bool):
        raise ValueError('A finite contribution is required')
    try:
        per_order = Fraction(str(per_order))
    except (ValueError, ZeroDivisionError) as exc:
        raise ValueError('A finite contribution is required') from exc
    # Negative contribution is possible and must not be silently discarded.
    return orders * per_order

def rate(completed, eligible):
    completed, eligible = count(completed), count(eligible)
    if eligible == 0 or completed > eligible:
        raise ValueError('Positive denominator and completed <= eligible required')
    return Fraction(completed, eligible)

def delivery_comparison(first, second):
    before, after = rate(*first), rate(*second)
    if before == 0:
        raise ValueError('Relative change from zero is undefined')
    return {'before':before, 'after':after, 'percentage_points':100*(after-before), 'relative_change':(after-before)/before}

class RequestFixture:
    """A teaching register: state does not survive restart; no concurrency claim."""
    def __init__(self):
        self.accepted = {}
    def record(self, request_id, order_id):
        if not all(isinstance(x,str) and x.strip() for x in [request_id,order_id]):
            raise ValueError('Nonempty identifiers required')
        if request_id in self.accepted:
            if self.accepted[request_id] != order_id:
                raise ValueError('Request identifier reused with conflicting order')
            return 'already recorded'
        self.accepted[request_id] = order_id
        return 'accepted'

def journey(completed, stopped, unsure):
    completed, stopped, unsure = map(count,(completed,stopped,unsure))
    return {'completed':completed,'stopped':stopped,'unsure':unsure,'completion_rate':rate(completed,completed+stopped+unsure)}

if __name__ == '__main__':
    print('Segment contributions:',contribution(10,200),contribution(8,250))
    print('Delivery comparison:',delivery_comparison((90,100),(96,120)))
    register=RequestFixture()
    for pair in [('R1','O1'),('R1','O1'),('R2','O2')]:
        print('Request:',pair,register.record(*pair))
    print('Accepted count:',len(register.accepted))
    print('Journey outcomes:',journey(9,2,1))
