#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../../../.." && pwd -P)"
DISPATCHER="$ROOT/modules/workstation/claude/bin/e2e-k8s-dispatch"
TEMP_DIR="$(mktemp -d)"
trap 'rm -rf -- "$TEMP_DIR"' EXIT

fail() {
    printf 'FAIL: %s\n' "$1" >&2
    exit 1
}

assert_stub() {
    local outcome="$1" expected_exit="$2" expected_receipt="$3"
    local receipt="$TEMP_DIR/$outcome.receipt" status
    set +e
    E2E_REMOTE_RECEIPT="$receipt" bash "$DISPATCHER" --stub "$outcome" >"$TEMP_DIR/$outcome.out" 2>"$TEMP_DIR/$outcome.err"
    status=$?
    set -e
    [[ "$status" -eq "$expected_exit" ]] || fail "--stub $outcome exited $status, expected $expected_exit"
    [[ -f "$receipt" ]] || fail "--stub $outcome did not write a receipt"
    [[ "$(<"$receipt")" == "$expected_receipt" ]] || fail "--stub $outcome wrote an unexpected receipt"
}

assert_stub success 0 $'client\t0'
assert_stub server-death 4 $'admission\t4'
assert_stub timeout 5 $'admission\t5'
assert_stub setup-reject 6 $'admission\t6'

set +e
E2E_REMOTE_RECEIPT="$TEMP_DIR/bogus.receipt" bash "$DISPATCHER" --stub bogus >"$TEMP_DIR/bogus.out" 2>"$TEMP_DIR/bogus.err"
bogus_status=$?
set -e
[[ "$bogus_status" -eq 2 ]] || fail "--stub bogus exited $bogus_status, expected 2"

bash -n "$DISPATCHER" || fail "dispatcher failed bash -n"

assert_rejection_pair() {
    local marker="$1" label="$2"
    local block
    block="$(grep -F -A4 -- "$marker" "$DISPATCHER" || true)"
    [[ "$block" == *"printf 'e2e-k8s-dispatch: nothing ran"* ]] || fail "$label rejection lacks a plain-English stderr message"
    [[ "$block" == *"set_result admission 6"* ]] || fail "$label rejection does not set admission 6"
}

assert_rejection_pair 'remote_node_check "$NODE" mirror "$MIRROR_SLUG"' mirror-absent
assert_rejection_pair 'remote_node_check "$NODE" image "$RUNNER_IMAGE"' image-absent
assert_rejection_pair 'render_manifest "$rendered_manifest"' render-failure

assert_indeterminate_pair() {
    local marker="$1" message="$2" label="$3"
    local block
    block="$(grep -F -A5 -- "$marker" "$DISPATCHER" || true)"
    [[ "$block" == *"$message"* ]] || fail "$label lacks its indeterminate-result stderr message"
    [[ "$block" == *"set_result admission 4"* ]] || fail "$label does not set admission 4"
    [[ "$block" == *"exit 4"* ]] || fail "$label does not exit 4"
}

assert_indeterminate_pair 'wait_output="$(remote_control wait' \
    'lost contact with Kubernetes before it could read the result; the run may or may not have completed' \
    wait-transport-failure
assert_indeterminate_pair 'returned a result the dispatcher could not parse' \
    'treat this run as indeterminate and inspect the output above' \
    malformed-result
assert_indeterminate_pair 'reported an impossible client exit status' \
    'treat this run as indeterminate' \
    invalid-client-status
assert_indeterminate_pair 'reported an unrecognised admission status' \
    'treat this run as indeterminate' \
    unknown-admission-status

post_submit_section="$(awk '
    found_apply && /^fi$/ { after_apply = 1; next }
    after_apply { print }
    /remote_control apply "\$manifest_b64"/ { found_apply = 1 }
' "$DISPATCHER")"
[[ -n "$post_submit_section" ]] || fail "could not locate the post-submit dispatcher section"
if grep -Fq 'nothing ran' <<< "$post_submit_section"; then
    fail 'post-submit dispatcher section still claims nothing ran'
fi

printf 'PASS: e2e-k8s-dispatch exit-code contract\n'
