#!/bin/bash

# BookingPro Local Setup Script for macOS
# This script automates the setup process for running BookingPro locally

echo "🚀 Starting BookingPro Local Setup..."

# Check if Homebrew is installed
if ! command -v brew &> /dev/null; then
    echo "📦 Installing Homebrew..."
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
    echo "✅ Homebrew already installed"
fi

# Install Node.js
if ! command -v node &> /dev/null; then
    echo "📦 Installing Node.js..."
    brew install node@20
else
    echo "✅ Node.js already installed"
fi

# Install MySQL
if ! command -v mysql &> /dev/null; then
    echo "📦 Installing MySQL..."
    brew install mysql
    echo "🔧 Starting MySQL service..."
    brew services start mysql
else
    echo "✅ MySQL already installed"
    # Ensure MySQL is running
    brew services start mysql
fi

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

# Create .env file if it doesn't exist
if [ ! -f .env ]; then
    echo "📝 Creating .env file..."
    cat > .env << EOL
# Database Configuration
DATABASE_URL="mysql://root:@localhost:3306/bookingpro"

# Application Configuration
NODE_ENV=development
PORT=5000

# Optional: SMTP Settings
SMTP_HOST=
SMTP_PORT=587
SMTP_USER=
SMTP_PASS=
EOL
    echo "⚠️  Please update the DATABASE_URL in .env with your MySQL root password"
else
    echo "✅ .env file already exists"
fi

# Setup MySQL database
echo "🗄️  Setting up MySQL database..."
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS bookingpro;"

# Check if mysql-setup.sql exists and import it
if [ -f mysql-setup.sql ]; then
    echo "📊 Importing database schema and sample data..."
    mysql -u root -p bookingpro < mysql-setup.sql
    echo "✅ Database setup complete!"
else
    echo "⚠️  mysql-setup.sql not found. Please run it manually after setup."
fi

echo ""
echo "🎉 Setup Complete!"
echo ""
echo "To start the application:"
echo "  npm run dev"
echo ""
echo "Access the application at:"
echo "  Frontend: http://localhost:5173"
echo "  Backend:  http://localhost:5000"
echo ""
echo "Admin Login:"
echo "  Email:    admin@bookingpro.com"
echo "  Password: admin123"
echo ""
echo "Don't forget to update your .env file with the correct MySQL password!"