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

echo "🔧 Ensuring nightly rustfmt is available..."
if ! rustup toolchain list | grep -q "^nightly"; then
  rustup toolchain install nightly >/dev/null
fi
rustup component add rustfmt --toolchain nightly >/dev/null

echo "🧹 Checking formatting with rustfmt..."
cargo +nightly fmt --all -- --check

echo "🔍 Running clippy (all features, all targets)..."
cargo clippy --workspace --all-targets --all-features -- -D warnings

echo "🧪 Running tests (all features)..."
cargo test --workspace --all-features

# Uncomment if you want to validate SQLx offline data
# echo "📦 Validating SQLx prepare..."
# cargo sqlx prepare --check --workspace

# same deterministic env
export TZ=UTC
export LC_ALL=C.UTF-8
export NO_COLOR=1
export CARGO_TERM_COLOR=never
export SOURCE_DATE_EPOCH=0

# Generate
./.github/scripts/gen_readme.sh

# Stage README if changed
if ! git diff --quiet -- README.md; then
  git add README.md
fi

echo "✅ All checks passed!"

