#!/bin/bash
set -e

# Script to set up local development environment

echo "Setting up WCAG-ADA local development environment..."

# Get the script directory
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
WCAG_ROOT=$(cd "$SCRIPT_DIR/.." && pwd)

# Check if .env exists
if [ ! -f "$WCAG_ROOT/.env" ]; then
  echo "Creating .env file from template..."
  cp "$WCAG_ROOT/.env.example" "$WCAG_ROOT/.env"
  echo "Please edit .env file with your configuration values"
fi

# Start development databases
echo "Starting development databases..."
cd "$WCAG_ROOT"
docker-compose -f docker-compose.dev.yml up -d

# Wait for databases to be ready
echo "Waiting for databases..."
sleep 5

# Install dependencies
echo "Installing dependencies..."
bun install

# Run database migrations
echo "Running database migrations..."
cd "$WCAG_ROOT/api"
bunx prisma migrate dev

# Start services
echo ""
echo "Development environment ready!"
echo ""
echo "To start all services, run:"
echo "  cd $WCAG_ROOT && bun run dev"
echo ""
echo "Or start individual services:"
echo "  API:       cd $WCAG_ROOT/api && bun run dev"
echo "  Worker:    cd $WCAG_ROOT/worker && bun run dev"
echo "  Dashboard: cd $WCAG_ROOT/dashboard && bun run dev"
echo ""
echo "Database tools:"
echo "  Prisma Studio: cd $WCAG_ROOT/api && bunx prisma studio"
echo "  Redis CLI:     docker exec -it wcag-ada-redis-dev redis-cli"
echo "  PostgreSQL:    docker exec -it wcag-ada-postgres-dev psql -U postgres -d wcag_ada_dev"