#!/bin/sh
# Independently authored, stateful fake for the reviewed JetBrains Central CLI surface. It intentionally
# models only argv, exit status, and the opaque global extension postcondition used by ThinkRail tests.
set -eu

# The control file holds space-separated tokens, so independent axes compose: the version a probe reports,
# whether credentials exist, and how an action fails are separate facts about a host, and a single-valued
# control could not express combinations a user really hits (an outdated Central whose update also fails).
state=""
if [ -n "${CENTRAL_STUB_STATE:-}" ] && [ -f "$CENTRAL_STUB_STATE" ]; then
	state=$(cat "$CENTRAL_STUB_STATE")
fi
has() {
	case " $state " in
	*" $1 "*) return 0 ;;
	*) return 1 ;;
	esac
}
drop() {
	next=""
	for token in $state; do
		if [ "$token" != "$1" ]; then next="$next $token"; fi
	done
	state=${next# }
	printf '%s\n' "$state" > "$CENTRAL_STUB_STATE"
}
if [ -n "${CENTRAL_STUB_LOG:-}" ]; then
	printf '%s\n' "$*" >> "$CENTRAL_STUB_LOG"
fi

artifact="$HOME/.pi/agent/extensions/jetbrains-central.ts"

if [ "$1" = "--version" ]; then
	if has probe-error; then
		printf 'E2E_CENTRAL_CHILD_SENTINEL_probe\n' >&2
		exit 7
	elif has malformed; then
		printf 'synthetic malformed version\n'
	elif has outdated; then
		printf 'central 1.3.9 (synthetic test metadata)\n'
	elif has newer; then
		printf 'central 1.7.0 (synthetic test metadata)\n'
	else
		printf 'central 1.6.2 (synthetic test metadata)\n'
	fi
	exit 0
fi

# Only the Auth/Proxy rows' *shape* is modelled: styled indicators, padded labels, and styled values. The
# strings are test-owned, and the extra line asserts ThinkRail never surfaces this output.
if [ "$1" = "status" ]; then
	if has slow-status && ! has needs-login; then
		sleep 1
	fi
	if has needs-login; then
		printf '\033[38;2;198;40;40m\xe2\xa0\xa4\033[m \033[1mAuth      \033[m \033[1;38;2;198;40;40mnot connected\033[m\n'
	else
		printf '\033[38;2;46;125;50m\xe2\xa3\xbf\033[m \033[1mAuth      \033[m \033[1;38;2;46;125;50mE2E Synthetic Access\033[m\n'
	fi
	if has proxy-stopped; then
		printf '\033[38;2;198;40;40m\xe2\xa0\xa4\033[m \033[1mProxy     \033[m \033[1;38;2;198;40;40mstopped\033[m\n'
	else
		printf '\033[38;2;46;125;50m\xe2\xa3\xbf\033[m \033[1mProxy     \033[m \033[1;38;2;46;125;50mrunning on port 19516\033[m\n'
	fi
	printf 'E2E_CENTRAL_CHILD_SENTINEL_status\n'
	exit 0
fi

if [ "$1" = "quota" ] && [ "${2:-}" = "--json" ]; then
	if has slow-quota; then
		sleep 1
	fi
	if has quota-error; then
		printf 'E2E_CENTRAL_CHILD_SENTINEL_quota_error\n' >&2
		exit 16
	fi
	if has quota-invalid; then
		printf '{"diagnostic":"E2E_CENTRAL_CHILD_SENTINEL_quota_invalid"}\n'
	elif has quota-alt; then
		printf '{"email":"E2E_CENTRAL_CHILD_SENTINEL_account","tariffQuota":{"available":"18.50","maximum":"20.00"},"topUpQuota":{"available":"99.00"},"diagnostic":"E2E_CENTRAL_CHILD_SENTINEL_quota"}\n'
	else
		printf '{"email":"E2E_CENTRAL_CHILD_SENTINEL_account","tariffQuota":{"available":"19.92","maximum":"20.00"},"topUpQuota":{"available":"99.00"},"diagnostic":"E2E_CENTRAL_CHILD_SENTINEL_quota"}\n'
	fi
	exit 0
fi

if [ "${CENTRAL_STUB_READ_ONLY:-}" = "1" ]; then
	exit 15
fi

if [ "$1" = "add" ] && [ "${2:-}" = "pi" ]; then
	if has needs-login || has add-error; then
		printf 'E2E_CENTRAL_CHILD_SENTINEL_add\n' >&2
		exit 9
	fi
	if has missing-artifact; then
		rm -f "$artifact"
		exit 0
	fi
	if has slow-add; then
		sleep 1
	fi
	mkdir -p "$(dirname "$artifact")"
	if has candidate-error; then
		cp "$CENTRAL_STUB_BAD_EXTENSION_SOURCE" "$artifact"
	else
		cp "$CENTRAL_STUB_EXTENSION_SOURCE" "$artifact"
	fi
	exit 0
fi

if [ "$1" = "remove" ] && [ "${2:-}" = "pi" ]; then
	if has remove-error; then
		printf 'E2E_CENTRAL_CHILD_SENTINEL_remove\n' >&2
		exit 11
	fi
	if ! has remove-leaves-artifact; then
		rm -f "$artifact"
	fi
	exit 0
fi

if [ "$1" = "proxy" ] && [ "${2:-}" = "start" ] && [ "${3:-}" = "--ensure-updated" ]; then
	if has proxy-start-error; then
		exit 14
	fi
	drop proxy-stopped
	exit 0
fi

if [ "$1" = "login" ]; then
	if has login-error; then
		exit 12
	fi
	# A successful synthetic login changes only the credential axis.
	if has needs-login; then
		drop needs-login
	fi
	exit 0
fi

if [ "$1" = "update" ] && [ "${2:-}" = "--install" ]; then
	if has update-error; then
		exit 13
	fi
	# A successful update makes the host supported again without changing auth/proxy axes.
	drop outdated
	exit 0
fi

exit 8
