#!/usr/bin/env bash
# Run: lint, test, compile, then deploy (dry-run by default).
# Usage: ./scripts/run-lint-test-build-deploy.sh [--deploy-live]
set -e
PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$PROJECT_ROOT"
export FOUNDRY_PROFILE=default
FORGE_SCOPE="${FORGE_SCOPE:-full}"

echo "=== 1. Compile ==="
bash scripts/forge/scope.sh build "$FORGE_SCOPE"
echo "✅ Build OK"

echo ""
echo "=== 2. Lint ==="
forge lint 2>&1 | head -100 || true
echo "✅ Lint done"

echo ""
echo "=== 3. Test ==="
bash scripts/forge/scope.sh test "$FORGE_SCOPE" 2>&1 | tail -50
echo "✅ Tests done"

echo ""
echo "=== 4. Deploy ==="
if [[ "${1:-}" == "--deploy-live" ]]; then
  echo "Live deploy: run your preferred deploy script, e.g.:"
  echo "  bash scripts/deployment/deploy-tokens-and-weth-all-chains-skip-canonical.sh"
  echo "  or: bash scripts/forge/scope.sh script script/deploy/DeployCWTokens.s.sol:DeployCWTokens --rpc-url \$RPC_URL --chain-id 138 --broadcast --private-key \$PRIVATE_KEY --legacy"
  exit 0
fi
echo "Dry-run: DeployCWTokens (simulation only)"
bash scripts/forge/scope.sh script script/deploy/DeployCWTokens.s.sol:DeployCWTokens --rpc-url "https://eth.llamarpc.com" --chain-id 1 2>&1 | tail -30
echo "✅ Deploy dry-run done (no broadcast)"
