#!/bin/bash

echo "🏇 Setting up Horse Racing Scraper..."

# Check for required tools
command -v node >/dev/null 2>&1 || { echo "❌ Node.js is required but not installed. Aborting."; exit 1; }
command -v npm >/dev/null 2>&1 || { echo "❌ npm is required but not installed. Aborting."; exit 1; }
command -v docker >/dev/null 2>&1 || { echo "⚠️  Docker not found. You'll need to set up PostgreSQL and Redis manually."; }

echo "✅ Required tools found"

# Install dependencies
echo "📦 Installing dependencies..."
npm install

# Start Docker containers if Docker is available
if command -v docker-compose >/dev/null 2>&1; then
  echo "🐳 Starting Docker containers..."
  docker-compose up -d
  
  echo "⏳ Waiting for databases to be ready..."
  sleep 10
fi

# Copy env file if it doesn't exist
if [ ! -f .env ]; then
  echo "📝 Creating .env file from template..."
  cp .env.example .env
  echo "⚠️  Please edit .env with your API keys and configuration"
fi

# Generate Prisma client
echo "🔧 Generating Prisma client..."
npm run prisma:generate

# Run migrations
echo "🗄️  Running database migrations..."
npm run prisma:migrate

echo ""
echo "✅ Setup complete!"
echo ""
echo "Next steps:"
echo "1. Edit .env with your API keys"
echo "2. Run 'npm run dev' to start development server"
echo "3. Open http://localhost:5555 for Prisma Studio (database GUI)"
echo ""
