#!/usr/bin/env bash
. "$(dirname "${BASH_SOURCE[0]}")/lib/harness.sh"
set -eo pipefail

TACHYON_BIN="$ROOT_DIR/tachyon/files/usr/bin/tachyon"
CLI_UC="$TACHYON_BIN"
HELPERS_SH="$TACHYON_LIB/helpers.sh"
LIFECYCLE_UC="$TACHYON_LIB/service/lifecycle.uc"
PACKAGES_UC="$TACHYON_LIB/core/packages.uc"
RULES_UC="$TACHYON_LIB/providers/rules.uc"
TACHYON_CORE_RUNTIME_UC="$TACHYON_LIB/tachyon_core/runtime.uc"
COMPONENT_ACTION_UC="$TACHYON_LIB/components/action.uc"
DIAGNOSTICS_RUNTIME_UC="$TACHYON_LIB/diagnostics/runtime.uc"

[ ! -e "$HELPERS_SH" ] ||
  fail "helpers.sh shell owner must be removed"
if grep -R -n -F 'helpers.sh' "$TACHYON_BIN" "$TACHYON_LIB" --include='*.sh' >/dev/null 2>&1; then
  fail "runtime shell must not reference helpers.sh"
fi
if grep -R -n -E 'helpers_ucode\(|get_(inbound|server_inbound|tailscale_dns_server|outbound)_tag_by_section\(|get_domain_resolver_tag\(|provider_status_ucode\(|is_ipv4\(|is_min_package_version\(|url_get_(scheme|userinfo|host|port|path|query_param)\(' \
  "$TACHYON_BIN" "$TACHYON_LIB" --include='*.sh' >/dev/null 2>&1; then
  fail "helpers.sh wrapper symbols must not remain in runtime shell"
fi

grep -Fq '#!/usr/bin/ucode' "$TACHYON_BIN" ||
  fail "tachyon entrypoint must be a direct ucode executable"
grep -Fq 'service/lifecycle.uc' "$CLI_UC" ||
  fail "service/cli.uc must dispatch lifecycle through service/lifecycle.uc"
grep -Fq 'core/packages.uc' "$LIFECYCLE_UC" ||
  fail "service/lifecycle.uc must use core/packages.uc directly"
for shell_owner_pattern in \
  'config_load' \
  'config_get' \
  'uci_set'
do
  if grep -Fq "$shell_owner_pattern" "$TACHYON_BIN"; then
    fail "tachyon shell entrypoint must not own $shell_owner_pattern logic"
  fi
done
if grep -E -n '(^|[;&|[:space:]])nft[[:space:]]+(list|add|delete|flush)' "$TACHYON_BIN" >/dev/null 2>&1; then
  fail "tachyon shell entrypoint must not own nft command logic"
fi
if grep -E -n '(^|[;&|[:space:]])ip[[:space:]]+(rule|route)' "$TACHYON_BIN" >/dev/null 2>&1; then
  fail "tachyon shell entrypoint must not own ip rule/route logic"
fi
[ ! -e "$TACHYON_LIB/updater.sh" ] ||
  fail "updater.sh shell owner must be removed"
[ ! -e "$TACHYON_LIB/status_diagnostics.sh" ] ||
  fail "status_diagnostics.sh shell owner must be removed"
grep -Fq 'diagnostics/runtime.uc' "$CLI_UC" ||
  fail "service/cli.uc must dispatch diagnostics through diagnostics/runtime.uc"
grep -Fq 'mode == "get-system-info"' "$DIAGNOSTICS_RUNTIME_UC" ||
  fail "diagnostics/runtime.uc must own system info"
grep -Fq 'core/packages.uc' "$TACHYON_LIB/components/helpers.uc" ||
  fail "component action owner must use core/packages.uc directly"
grep -Fq 'count-uci' "$RULES_UC" ||
  fail "providers/rules.uc must own UCI rule counting"
for mode in \
  'mode == "version"' \
  'mode == "version-output"' \
  'mode == "version-from-output"' \
  'mode == "read-version-state"' \
  'mode == "write-version-state"' \
  'mode == "restore-version-state"' \
  'mode == "supports-xhttp"' \
  'mode == "supports-tailscale"' \
  'mode == "supports-cert-pin"' \
  'mode == "core-profile"' \
  'mode == "core-capabilities"'
do
  grep -Fq "$mode" "$TACHYON_CORE_RUNTIME_UC" ||
    fail "tachyon_core/runtime.uc missing $mode"
done
if grep -R -n -E 'is_core_installed\(|core_version_output\(|core_supports_tailscale\(' \
  "$TACHYON_BIN" "$TACHYON_LIB" --include='*.sh' >/dev/null 2>&1; then
  fail "core helper/state shell symbols must not remain"
fi

if ucode -L "$TACHYON_LIB" "$PACKAGES_UC" installed tachyon-definitely-missing >/dev/null 2>&1; then
  fail "missing package must not be reported installed"
fi

mkdir -p "$WORK_DIR/apk-bin"
cat >"$WORK_DIR/apk-bin/apk" <<'SH'
#!/usr/bin/env sh
if [ "$*" = "query --from repositories --available --format json --fields name,version tachyon-core" ]; then
  printf '%s\n' '[{"name":"tachyon-core","version":"1.2.3-r1"}]'
  exit 0
fi
exit 1
SH
chmod +x "$WORK_DIR/apk-bin/apk"
[ "$(PATH="$WORK_DIR/apk-bin:$PATH" ucode -L "$TACHYON_LIB" "$PACKAGES_UC" apk-available-version tachyon-core)" = "1.2.3-r1" ] ||
  fail "APK available version lookup must select the exact package name, not a higher-priority provider"
mark_hex="$(ucode -L "$TACHYON_LIB" "$RULES_UC" mark-hex 0x01000000 2)"
[ "$mark_hex" = "0x01000002" ] ||
  fail "providers/rules.uc mark math changed"

version="$(printf 'tachyon-core version 0.5.7\nEnvironment: test\n' |
  ucode -L "$TACHYON_LIB" "$TACHYON_CORE_RUNTIME_UC" version-from-output)"
[ "$version" = "0.5.7" ] ||
  fail "tachyon_core/runtime.uc version-from-output changed"

TACHYON_CORE_VERSION_FILE="$WORK_DIR/version" \
  ucode -L "$TACHYON_LIB" "$TACHYON_CORE_RUNTIME_UC" write-version-state 0.5.7 >/dev/null ||
  fail "version state write failed"
[ "$(TACHYON_CORE_VERSION_FILE="$WORK_DIR/version" ucode -L "$TACHYON_LIB" "$TACHYON_CORE_RUNTIME_UC" read-version-state)" = "0.5.7" ] ||
  fail "version state read failed"
TACHYON_CORE_VERSION_FILE="$WORK_DIR/version" \
  ucode -L "$TACHYON_LIB" "$TACHYON_CORE_RUNTIME_UC" restore-version-state "" >/dev/null ||
  fail "version state restore-clear failed"
[ ! -e "$WORK_DIR/version" ] ||
  fail "empty version state restore must clear the state file"

printf 'helpers ownership checks passed\n'
