#!/bin/bash
set -e

echo "🚀 Starting Laravel container (Simple Mode)..."

# Navigate to Laravel root
cd /var/www/html

# Create essential directories
mkdir -p storage/logs storage/framework/cache storage/framework/sessions storage/framework/views bootstrap/cache

# Set basic permissions
chmod -R 775 storage bootstrap/cache 2>/dev/null || true

# Create storage symlink if it doesn't exist
if [ ! -L "public/storage" ]; then
    rm -rf public/storage 2>/dev/null || true
    ln -sf /var/www/html/storage/app/public public/storage
fi

# Copy .env if it doesn't exist
if [ ! -f ".env" ] && [ -f ".env.example" ]; then
    cp .env.example .env
fi

# Configure PHP-FPM for Docker networking
sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /usr/local/etc/php-fpm.d/www.conf 2>/dev/null || true

echo "✅ Basic setup complete"
echo "🚀 Starting PHP-FPM..."

# Start PHP-FPM in foreground
exec php-fpm --nodaemonize --fpm-config /usr/local/etc/php-fpm.conf