"""Aurora Lab 001: ordering semantics, using synthetic data only.

Run with Python 3: python3 event_order_lab.py
Contract: a single sequence scope; SET overwrites remaining quantity.
This is an educational example, not a production feed handler.

MIT License
Copyright (c) 2026 Unique Aurora
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

EVENTS = (
    {"sequence": 1, "source_ms": 1000, "received_ms": 1100, "quantity": 10},
    {"sequence": 2, "source_ms": 1001, "received_ms": 1103, "quantity": 4},
    {"sequence": 3, "source_ms": 1002, "received_ms": 1101, "quantity": 7},
)


def replay(order_key):
    """Return the ordered trace; no network, files or trading operations."""
    if order_key not in ("sequence", "received_ms"):
        raise ValueError("Unsupported ordering key")
    return [
        (event["sequence"], event["quantity"])
        for event in sorted(EVENTS, key=lambda event: event[order_key])
    ]


if __name__ == "__main__":
    source = replay("sequence")
    received = replay("received_ms")
    assert source == [(1, 10), (2, 4), (3, 7)]
    assert received == [(1, 10), (3, 7), (2, 4)]
    print("Source sequence:", source, "Final quantity:", source[-1][1])
    print("Receive time:   ", received, "Final quantity:", received[-1][1])
    print("The stated contract makes source sequence authoritative.")
