#!/usr/bin/env bash

set -euo pipefail

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
fixture=$(mktemp -d)
trap 'rm -rf "$fixture"' EXIT
root="$fixture/platform"
old="$root/releases/old-release"
mkdir -p "$old/web/assets" "$old/deploy" "$root/env" "$fixture/bin"
printf '#!/usr/bin/env bash\nexit 0\n' > "$old/platform-api"
printf 'old evaluator\n' > "$old/alert-evaluator"
chmod +x "$old/platform-api"
cp "$SCRIPT_DIR/verify-web-release.sh" "$old/deploy/verify-web-release.sh"
printf 'old.js\n' > "$old/web/.release-assets"
printf 'old\n' > "$old/web/assets/old.js"
ln -s "$old" "$root/current"
printf 'PLATFORM_RELEASE=old-release\n' > "$root/env/platform.env"

for index in 1 2 3 4; do mkdir -p "$root/releases/stale-$index"; touch -t "20260716010${index}" "$root/releases/stale-$index"; done

new_web="$fixture/new-web"
mkdir -p "$new_web/assets"
printf 'new.js\n' > "$new_web/.release-assets"
printf 'new\n' > "$new_web/assets/new.js"
printf '<main>new</main>\n' > "$new_web/index.html"
printf 'window.__LINGNIU_APP_CONFIG__={"amapSecurityServiceHost":"/_AMapService"};\n' > "$new_web/app-config.js"
tar -C "$new_web" -czf "$fixture/new.tar.gz" .
printf 'new platform API\n' > "$fixture/new-platform-api"

printf '#!/usr/bin/env bash\nexit 0\n' > "$fixture/bin/systemctl"
printf '#!/usr/bin/env bash\nexit 0\n' > "$fixture/bin/curl"
printf '#!/usr/bin/env bash\nprintf "mock_verify=ok\\n"\n' > "$fixture/bin/verify"
chmod +x "$fixture/bin/"*

PLATFORM_ROOT="$root" SYSTEMCTL_BIN="$fixture/bin/systemctl" CURL_BIN="$fixture/bin/curl" VERIFY_WEB_RELEASE_BIN="$fixture/bin/verify" RELEASE_HISTORY_LIMIT=3 "$SCRIPT_DIR/install-web-release.sh" new-release "$fixture/new.tar.gz" "$fixture/new-platform-api" > "$fixture/install.out"
test "$(readlink -f "$root/current")" = "$(cd "$root/releases/new-release" && pwd -P)"
grep -q '^PLATFORM_RELEASE=new-release$' "$root/env/platform.env"
test "$(cat "$root/current/platform-api")" = 'new platform API'
test "$(cat "$root/current/alert-evaluator")" = 'old evaluator'
test -f "$root/current/web/assets/new.js"
test -f "$root/current/web/assets/old.js"
test -f "$root/current/web/.compatibility-manifests/1.assets"
test -x "$root/current/deploy/verify-customer-demo.py"
test -f "$root/current/deploy/migrations/015_vehicle_source_policy_audit.sql"
test ! -e "$root/current/lingniu-vehicle-platform.service"
test "$(find "$root/releases" -mindepth 1 -maxdepth 1 -type d | wc -l | tr -d ' ')" = 3
grep -q 'web_release_install=ok release=new-release previous=old-release' "$fixture/install.out"

rollback_web="$fixture/rollback-web"
mkdir -p "$rollback_web/assets"
printf 'rollback.js\n' > "$rollback_web/.release-assets"
printf 'rollback\n' > "$rollback_web/assets/rollback.js"
printf '<main>rollback</main>\n' > "$rollback_web/index.html"
printf 'window.__LINGNIU_APP_CONFIG__={"amapSecurityServiceHost":"/_AMapService"};\n' > "$rollback_web/app-config.js"
tar -C "$rollback_web" -czf "$fixture/rollback.tar.gz" .
printf '#!/usr/bin/env bash\nexit 1\n' > "$fixture/bin/fail-verify"
chmod +x "$fixture/bin/fail-verify"
if PLATFORM_ROOT="$root" SYSTEMCTL_BIN="$fixture/bin/systemctl" CURL_BIN="$fixture/bin/curl" VERIFY_WEB_RELEASE_BIN="$fixture/bin/fail-verify" RELEASE_HISTORY_LIMIT=3 "$SCRIPT_DIR/install-web-release.sh" broken-release "$fixture/rollback.tar.gz" > "$fixture/rollback.out" 2>&1; then
  printf 'expected failed verification to roll back\n' >&2
  exit 1
fi
test "$(readlink -f "$root/current")" = "$(cd "$root/releases/new-release" && pwd -P)"
grep -q '^PLATFORM_RELEASE=new-release$' "$root/env/platform.env"
grep -q 'current restored' "$fixture/rollback.out"

printf 'install-web-release tests passed\n'
