"""Authored, synthetic lifecycle exercise. No vendor, API, or customer data.
Run with Python 3.10+; stdout is the checked-in results.json.
Versions are an invented per-identity lifecycle sequence, not vendor timestamps.
"""
import copy
import json

INITIAL = {
    "C01": {"state": "active", "version": 1},
    "C02": {"state": "active", "version": 1},
}
EVENTS = [
    {"id": "E1", "key": "C01", "kind": "delete", "version": 2},
    {"id": "E2", "key": "C01", "kind": "upsert", "version": 1},
    {"id": "E3", "key": "C01", "kind": "delete", "version": 2},
    {"id": "E4", "key": "C02", "kind": "upsert", "version": 2},
    {"id": "E5", "key": "C01", "kind": "restore", "version": 3, "approved": False},
    {"id": "E6", "key": "C01", "kind": "restore", "version": 3, "approved": True},
    {"id": "E7", "key": "C01", "kind": "delete", "version": 2},
    {"id": "E8", "key": "C01", "kind": "upsert", "version": 4},
    {"id": "E9", "key": "C01", "kind": "upsert", "version": None},
]
EXPECTED = ["applied", "stale", "repeat", "applied", "review", "applied", "stale", "applied", "review"]

def run(protected):
    records = copy.deepcopy(INITIAL)
    rows = []
    seen = set()
    for event in EVENTS:
        current = records[event["key"]]
        marker = (event["key"], event["kind"], event["version"])
        if protected and (event["version"] is None or
                          (event["kind"] == "restore" and not event.get("approved"))):
            decision = "review"
        elif protected and marker in seen and event["version"] == current["version"]:
            decision = "repeat"
        elif protected and event["version"] <= current["version"]:
            decision = "stale"
        elif protected and current["state"] == "deleted" and event["kind"] == "upsert":
            decision = "review"
        else:
            decision = "applied"
            records[event["key"]] = {
                "state": "deleted" if event["kind"] == "delete" else "active",
                "version": event["version"],
            }
            seen.add(marker)
        # Lifecycle state intentionally contains no marketing eligibility field.
        rows.append({"event": event["id"], "decision": decision,
                     "record": copy.deepcopy(records[event["key"]])})
    return rows

protected = run(True)
naive = run(False)
assert [row["decision"] for row in protected] == EXPECTED
assert protected[1]["record"]["state"] == "deleted"
assert naive[1]["record"]["state"] == "active"
assert protected[6]["record"]["state"] == "active"
assert naive[6]["record"]["state"] == "deleted"
print(json.dumps({
    "method": "Authored nine-event local policy exercise; not a vendor test or benchmark",
    "version_assumption": "Single comparable, per-identity lifecycle sequence supplied by the fixture",
    "initial": INITIAL, "events": EVENTS, "expected_protected_decisions": EXPECTED,
    "naive": naive, "protected": protected,
    "observations": {
        "E2_naive_recreates_deleted_contact": True,
        "E7_naive_deletes_restored_contact": True,
        "protected_review_events": ["E5", "E9"],
        "marketing_eligibility_modeled": False,
    },
    "limitations": ["Invented records and ordered inputs", "No vendor or connector execution",
                    "No concurrent writers or clock reconciliation", "No relationship or file restoration",
                    "No identity matching, erasure, retention, or legal compliance test",
                    "Cannot establish absence of copies in other systems"],
}, indent=2) + "\n", end="")
