#!/bin/bash
# Local test script for GitHub Actions workflow

# Set working directory to the SDK root
cd "$(dirname "$0")/.."
SDK_ROOT=$(pwd)

echo "🚀 Testing GitHub Actions workflow locally for JavaScript SDK"
echo "📂 Working directory: $SDK_ROOT"

# Step 1: Install dependencies
echo "📦 Installing dependencies..."
npm ci
if [ $? -ne 0 ]; then
  echo "❌ Failed to install dependencies"
  exit 1
fi
echo "✅ Dependencies installed successfully"

# Step 2: Build the SDK
echo "🔨 Building SDK..."
npm run build
if [ $? -ne 0 ]; then
  echo "❌ Build failed"
  exit 1
fi
echo "✅ Build completed successfully"

# Step 3: Run tests
echo "🧪 Running tests..."
npm test
if [ $? -ne 0 ]; then
  echo "❌ Tests failed"
  exit 1
fi
echo "✅ Tests passed successfully"

# Step 4: Generate documentation
echo "📚 Generating documentation..."
npm run docs
if [ $? -ne 0 ]; then
  echo "❌ Documentation generation failed"
  exit 1
fi
echo "✅ Documentation generated successfully"

# Step 5: Simulate version bump (dry run)
echo "🏷️ Simulating version bump (dry run)..."
npm version patch --no-git-tag-version --dry-run
if [ $? -ne 0 ]; then
  echo "❌ Version bump simulation failed"
  exit 1
fi
echo "✅ Version bump simulation successful"

# Step 6: Simulate npm publish (dry run)
echo "📦 Simulating npm publish (dry run)..."
npm publish --dry-run
if [ $? -ne 0 ]; then
  echo "❌ Publish simulation failed"
  exit 1
fi
echo "✅ Publish simulation successful"

echo "🎉 All GitHub Actions workflow steps passed locally!"
echo "👉 You can now push your changes and create a tag to trigger the actual workflow"