#!/bin/bash

echo "========================================"
echo "  WA Gateway - Database Setup"
echo "========================================"
echo ""

# Check if MySQL is running
echo "[1/3] Checking MySQL connection..."
if ! mysql -u root -e "SELECT 1" &> /dev/null; then
    echo "ERROR: MySQL is not running or not accessible"
    echo "Please start your MySQL server first"
    exit 1
fi
echo "OK: MySQL is running"
echo ""

# Run database setup command
echo "[2/3] Setting up database..."
if ! php artisan db:setup --fresh --seed; then
    echo "ERROR: Database setup failed"
    exit 1
fi
echo "OK: Database setup completed"
echo ""

# Clear cache
echo "[3/3] Clearing cache..."
php artisan config:clear
php artisan cache:clear
php artisan view:clear
echo "OK: Cache cleared"
echo ""

echo "========================================"
echo "  Setup Complete!"
echo "========================================"
echo ""
echo "Start the server:"
echo "  php artisan serve"
echo ""
echo "Then visit: http://localhost:8000"
echo ""
