#!/usr/bin/env bash
# Mock `iris` CLI for phase2 tests. Reads fixtures from $MOCK_FIXTURES_DIR
# (set by the test runner). Logs each invocation to $MOCK_IRIS_LOG so the
# test can assert on call patterns.
#
# Supported (the global --json flag may precede the subcommand):
#   iris --json export diagram <id> --format json  → fixtures/diagram-export.json
#   iris --json elements get <id>                  → fixtures/element-<id>.json
#   iris update element <id> --data-json X          → no-op, exit 0

set -uo pipefail

: "${MOCK_FIXTURES_DIR:?MOCK_FIXTURES_DIR must be set}"
: "${MOCK_IRIS_LOG:?MOCK_IRIS_LOG must be set}"

echo "iris $*" >> "$MOCK_IRIS_LOG"

# Drop a leading global --json flag so positional matching is stable.
[ "${1:-}" = "--json" ] && shift

case "${1:-} ${2:-}" in
    "export diagram")
        cat "$MOCK_FIXTURES_DIR/diagram-export.json"
        ;;
    "elements get")
        element_id="$3"
        case "$element_id" in
            11111111-*) cat "$MOCK_FIXTURES_DIR/element-cache-hit.json" ;;
            22222222-*) cat "$MOCK_FIXTURES_DIR/element-cached-fail.json" ;;
            33333333-*) cat "$MOCK_FIXTURES_DIR/element-no-cache.json" ;;
            44444444-*) cat "$MOCK_FIXTURES_DIR/element-no-product-attr.json" ;;
            *)
                echo "mock iris: unknown element $element_id" >&2
                exit 1
                ;;
        esac
        ;;
    "update element")
        # Accept and discard the JSON payload — the test asserts on the call
        # count via the log, not the PUT body.
        exit 0
        ;;
    *)
        echo "mock iris: unsupported invocation '$*'" >&2
        exit 2
        ;;
esac
