#!/bin/bash
set -e

# Wait for MySQL to be ready
echo "Waiting for MySQL..."
while ! mysqladmin ping -h batik_db -uroot -proot --ssl=0 --silent; do
    sleep 1
done
echo "MySQL is ready!"

cd /var/www/html

# Clear any cached configurations that might confuse Laravel
php artisan config:clear
php artisan route:clear
php artisan view:clear

# Run migrations and seed the database
echo "Running migrations..."
php artisan migrate:fresh --force

echo "Seeding database..."
php artisan db:seed --force

# Create storage link if it doesn't exist
php artisan storage:link || true

echo "Starting Apache..."
exec "$@"
