#!/usr/bin/env python3
"""Synthetic in-memory model of three hook shapes and a separate trust check.

This is not Muse's validator. It recognizes only the two events and the exact
handler form needed for this example. It never executes command strings,
installs software, opens a network connection, or edits real configuration.
"""

import json


SHAPES = [
    {"hooks": [
        {"event": "SessionStart", "command": ["sh", "capture.sh"]},
        {"event": "Stop", "command": ["sh", "capture.sh"]},
    ]},
    {"hooks": {
        "SessionStart": [{"command": ["sh", "capture.sh"]}],
        "Stop": [{"command": ["sh", "capture.sh"]}],
    }},
    {"hooks": {
        "SessionStart": [{"matcher": "*", "hooks": [
            {"type": "command", "command": "sh capture.sh"}]}],
        "Stop": [{"matcher": "*", "hooks": [
            {"type": "command", "command": "sh capture.sh"}]}],
    }},
]


def recognize_shape(config):
    events = config.get("hooks")
    if not isinstance(events, dict):
        return False, "toy model: hooks must be an event map"
    if set(events) != {"SessionStart", "Stop"}:
        return False, "toy model: this example recognizes exactly two events"
    for groups in events.values():
        if not isinstance(groups, list) or not groups:
            return False, "toy model: each event needs a nonempty group list"
        for group in groups:
            if not isinstance(group, dict) or set(group) != {"matcher", "hooks"}:
                return False, "toy model: group needs matcher and nested hooks"
            if group["matcher"] != "*":
                return False, "toy model: only the fixture's wildcard is modeled"
            handlers = group["hooks"]
            if not isinstance(handlers, list) or not handlers:
                return False, "toy model: a group needs nonempty handlers"
            for handler in handlers:
                if not isinstance(handler, dict) or set(handler) != {"type", "command"}:
                    return False, "toy model: handler needs type and command"
                if handler["type"] != "command" or not isinstance(handler["command"], str):
                    return False, "toy model: handler needs command type and string"
    return True, "toy model: recognized fixture shape"


def simulate(config, trusted):
    recognized, diagnostic = recognize_shape(config)
    eligible = recognized and trusted
    # These are data values, not evidence that any real command ran.
    simulated_events = []
    if eligible:
        for event, groups in config["hooks"].items():
            for group in groups:
                simulated_events.extend(event for _ in group["hooks"])
    return {
        "recognized_shape": recognized,
        "diagnostic": diagnostic,
        "trusted_input": trusted,
        "eligible_in_toy_model": eligible,
        "simulated_events": simulated_events,
        "real_commands_executed": 0,
    }


def main():
    rows = []
    for number, config, trusted in (
        (1, SHAPES[0], False), (2, SHAPES[1], False),
        (3, SHAPES[2], False), (4, SHAPES[2], True),
    ):
        rows.append({"case": number, "config": config, **simulate(config, trusted)})
    print(json.dumps({
        "classification": "SYNTHETIC_TEACHING_MODEL_NOT_A_MUSE_TEST",
        "normalization": "Historical probe paths replaced with capture.sh; structure retained.",
        "limits": "No event dispatch, matcher semantics, process execution, or security boundary is tested.",
        "cases": rows,
    }, indent=2))


if __name__ == "__main__":
    main()
