#!/bin/bash

# Grid Battle Game Startup Script
# Ensures all components are ready before starting

echo "🎮 Starting Grid Battle Game..."

# Check if Node.js is available
if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js first."
    exit 1
fi

# Check if npm is available
if ! command -v npm &> /dev/null; then
    echo "❌ npm is not available. Please install npm first."
    exit 1
fi

# Ensure we're in the correct directory
if [ ! -f "package.json" ]; then
    echo "❌ package.json not found. Make sure you're in the correct directory."
    exit 1
fi

# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
    echo "📦 Installing dependencies..."
    npm install
fi

# Create database directory if it doesn't exist
mkdir -p database

# Start the server
echo "🚀 Starting Grid Battle Game server..."
echo "📊 Database will be initialized automatically"
echo "🌐 Server will be available at http://localhost:8050"
echo "🎯 Press Ctrl+C to stop the server"
echo ""

npm start