"""Recreated support example using synthetic data, not production incident evidence.
Run with Python 3: python3 support-validation.py
"""
import sqlite3

def normalize(payload):
    reference = payload.get("reference") or payload.get("order_reference")
    if not isinstance(reference, str) or not reference.strip():
        raise ValueError("Missing order reference")
    cents = payload.get("cents")
    if type(cents) is not int or cents <= 0:
        raise ValueError("Amount must be positive integer cents")
    if payload.get("channel") not in ("POS", "Web", "Delivery"):
        raise ValueError("Unknown channel")
    return reference, payload["channel"], cents

fixture = {"order_reference": "SAMPLE-001", "channel": "POS", "cents": 2450}
assert fixture.get("reference") is None  # Reproduce the field-name mismatch.
row = normalize(fixture)
assert row == ("SAMPLE-001", "POS", 2450)
with sqlite3.connect(":memory:") as db:
    db.execute("CREATE TABLE orders(reference TEXT UNIQUE, channel TEXT, cents INTEGER)")
    db.execute("INSERT INTO orders VALUES (?, ?, ?)", row)
    assert db.execute("SELECT cents FROM orders WHERE reference=?", (row[0],)).fetchone() == (2450,)
for bad in ({"reference":"X","channel":"POS","cents":-1}, {"reference":"X","channel":"Unknown","cents":100}):
    try: normalize(bad)
    except ValueError: pass
    else: raise AssertionError("Invalid record accepted")
print("PASS: synthetic mapping reproduction, normalization, SQL round trip, and invalid-record rejection")
