# Development Dockerfile with FrankenPHP
FROM dunglas/frankenphp:latest-php8.3-alpine

# Install system dependencies + development tools
RUN apk add --no-cache \
    nodejs \
    npm \
    git \
    mysql-client \
    vim \
    bash \
    nano

# Install PHP extensions including xdebug for development
RUN install-php-extensions \
    pdo_mysql \
    opcache \
    zip \
    gd \
    intl \
    bcmath \
    redis \
    pcntl \
    xdebug

# Install Composer
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /app

# Configure PHP for development
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"

# Configure Xdebug (disabled by default to reduce noise)
RUN echo "xdebug.mode=off" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo ";xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo ";xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo ";xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

# Configure Caddy for development (simpler, no worker mode)
RUN cat > /etc/caddy/Caddyfile <<EOF
{
    frankenphp
    order php_server before file_server
}

:8000 {
    root * /app/public

    php_server {
        index index.php
    }

    encode gzip

    file_server

    # Less strict headers for development
    header {
        X-Frame-Options "SAMEORIGIN"
    }
}
EOF

# Install Node development dependencies globally
RUN npm install -g nodemon

# Create startup script for development
RUN cat > /start.sh <<'EOF'
#!/bin/sh
set -e

# Create .env file if it doesn't exist
if [ ! -f ".env" ]; then
    echo "Creating .env file from .env.example..."
    cp .env.example .env
fi

# Install dependencies if volumes are empty
if [ ! -f "vendor/autoload.php" ]; then
    echo "Installing composer dependencies..."
    composer install
fi

# Always reinstall node_modules in container to get correct native binaries for Alpine/musl
echo "Installing npm dependencies..."
rm -rf node_modules 2>/dev/null || true
rm -rf /app/.npm 2>/dev/null || true
npm install --cache /tmp/.npm

# Clear Laravel caches
php artisan config:clear || true
php artisan cache:clear || true

# Wait for database and run migrations
echo "Waiting for database..."
sleep 5
php artisan migrate --force || echo "Migration failed or not needed"

# Run seeders
echo "Running seeders..."
php artisan db:seed --force || echo "Seeding skipped or already done"

# Generate app key if not set
if [ -z "$APP_KEY" ] || [ "$APP_KEY" = "base64:YOUR_KEY_HERE" ]; then
    echo "Generating application key..."
    php artisan key:generate
fi

# Start Vite dev server in background
npm run dev &

# Start Horizon (queue worker) in background
php artisan horizon &

# Scheduler left off in dev on purpose; run schedule:work by hand when needed.
# php artisan schedule:work &

# Start FrankenPHP
exec frankenphp run --config /etc/caddy/Caddyfile
EOF

RUN chmod +x /start.sh

# Expose ports
EXPOSE 8000 5173

# Use the startup script
CMD ["/start.sh"]
