"""Conditional timing examples only; no SDK, application, or hardware is invoked."""
import json


def simulate(threshold, missing=(), removed_at=None):
    last_code = ""
    count = 0
    present = False
    removals = []
    trace = []
    for t in range(0, 1201, 100):
        physically_present = removed_at is None or t < removed_at
        if t % 300 == 0:
            last_code = "A" if physically_present and t not in missing else ""
        if last_code:
            count = 0
            present = True
        else:
            count += 1
            if present and count >= threshold:
                removals.append(t)
                present = False
        trace.append({"ms": t, "physical_present": physically_present,
                      "reported_code": last_code, "empty_count": count})
    return removals, trace


CASES = [
    ("stable-present", {"threshold": 3}, []),
    ("real-removal-at-50", {"threshold": 3, "removed_at": 50}, [500]),
    ("one-real-miss-n3", {"threshold": 3, "missing": [300]}, [500]),
    ("one-real-miss-n7", {"threshold": 7, "missing": [300]}, []),
    ("three-real-misses-n7", {"threshold": 7, "missing": [300, 600, 900]}, [900]),
]

if __name__ == "__main__":
    output = []
    for name, arguments, expected in CASES:
        observed, trace = simulate(**arguments)
        assert observed == expected, (name, observed, expected)
        output.append({"case": name, "result": "PASS", "removal_ms": observed,
                       "arguments": arguments, "trace": trace})
    print(json.dumps({"boundary": "Conditional model; not SDK/app/hardware evidence",
                      "count": len(output), "cases": output}, ensure_ascii=False, indent=2))
