#!/usr/bin/env bash
# Update Quaternions registry dependencies and run the full safety gate.
#
# Usage:
#   scripts/update_quaternions_deps.sh <klondike_version> <card_game_version>
#
# Example:
#   scripts/update_quaternions_deps.sh 0.3.1 0.4.1
#
# This script updates Cargo.lock to the requested versions (within the semver
# ranges already declared in Cargo.toml), then runs the project's required
# verification steps plus deterministic replay checks.
set -euo pipefail

if [ "$#" -ne 2 ]; then
  echo "usage: $0 <klondike_version> <card_game_version>"
  exit 2
fi

KLONDIKE_VERSION="$1"
CARD_GAME_VERSION="$2"
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

cd "$REPO_ROOT"

echo ">>> Quaternions registry:"
echo "    https://git.aleshym.co/api/packages/Quaternions/cargo/"
echo
echo ">>> Review upstream release notes / changelogs before proceeding:"
echo "    - https://git.aleshym.co/Quaternions/card_game"
echo "    - https://git.aleshym.co/Quaternions/klondike"
echo

echo ">>> Updating lockfile to klondike=$KLONDIKE_VERSION card_game=$CARD_GAME_VERSION"
cargo update -p klondike --precise "$KLONDIKE_VERSION"
cargo update -p card_game --precise "$CARD_GAME_VERSION"

echo ">>> Verifying dependency graph"
cargo tree -p solitaire_core --depth 2 | cat

echo ">>> Running workspace tests"
cargo test --workspace

echo ">>> Running workspace clippy"
cargo clippy --workspace -- -D warnings

echo ">>> Running deterministic replay / debug-api smoke checks"
cargo test -p solitaire_wasm debug_snapshot_exposes_replayable_seed_and_history -- --exact
cargo test -p solitaire_wasm debug_api_autonomous_seed_batch_smoke -- --exact

echo ">>> Quaternions dependency upgrade gate passed"
