"""Read synthetic teaching events; never contact a network or validate a real protocol."""
import argparse
import json
from pathlib import Path

MILESTONES = ["local_ip", "ike_initial", "eap_success", "child_ready", "core_ready", "service_ok"]
STATES = ["LOCAL_IP_READY", "IKE_INITIAL_EXCHANGE", "SUBSCRIBER_EAP_VERIFIED",
          "IKE_AUTHENTICATED_AND_CHILD_READY", "CORE_SESSION_AND_FORWARDING_READY", "SERVICE_WORKING"]
NEXT = ["initial IKE response / reachability evidence", "subscriber EAP result",
        "final IKE authentication and Child SA readiness", "core session and forwarding readiness",
        "service-specific success evidence", "none within this teaching model"]


def analyze(events):
    stage = -1
    previous_time = -1
    rejected = False
    allowed = set(MILESTONES) | {"challenge", "ike_request", "eap_reject", "capture_end"}
    for timestamp, event in events:
        if not isinstance(timestamp, (int, float)) or timestamp < previous_time:
            raise ValueError("Events require nondecreasing nonnegative timestamps.")
        previous_time = timestamp
        if event not in allowed:
            raise ValueError(f"Unknown teaching event: {event}")
        if event in MILESTONES:
            position = MILESTONES.index(event)
            if rejected or position != stage + 1:
                raise ValueError("Milestone order violates this teaching model (not a standards verdict).")
            stage = position
        if event == "eap_reject":
            if stage != 1:
                raise ValueError("Rejection is only modeled during the EAP stage.")
            rejected = True
    return {
        "highest_evidenced": STATES[stage] if stage >= 0 else "NONE",
        "next_evidence": "inspect the explicit EAP rejection cause" if rejected else NEXT[stage] if stage >= 0 else "local IP readiness",
        "explicit_rejection": rejected,
        "limit": "Missing evidence is not proof of failure. Synthetic model only."
    }


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("scenario", nargs="?")
    parser.add_argument("--list", action="store_true")
    args = parser.parse_args()
    data = json.loads(Path(__file__).with_name("auth-traces.json").read_text(encoding="utf-8"))
    scenarios = data["scenarios"]
    if args.list:
        print("\n".join(scenarios))
        return
    if args.scenario not in scenarios:
        parser.error("Choose a scenario from --list.")
    print(data["notice"])
    for time_ms, event in scenarios[args.scenario]:
        print(f"{time_ms:7} ms  {event}")
    print(json.dumps(analyze(scenarios[args.scenario]), indent=2))


if __name__ == "__main__":
    main()
