# Use PHP 8.2 Alpine (compatible with Laravel 11)
FROM php:8.2-fpm-alpine

# Install required dependencies
RUN apk add --no-cache \
    oniguruma-dev \
    libpng-dev \
    libjpeg-turbo-dev \
    libwebp-dev \
    freetype-dev \
    libzip-dev \
    zip \
    unzip \
    openssl  # Laravel requires OpenSSL for encryption

# Install required extensions
RUN docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install gd pdo pdo_mysql bcmath mbstring zip

# Install Composer (using official Composer image)
COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /var/www

# Copy Laravel application files BEFORE running composer install
COPY . /var/www/

# Ensure composer.json exists before running install
RUN if [ -f "composer.json" ]; then composer install --no-dev --optimize-autoloader; else echo "composer.json not found!"; fi

# Ensure required Laravel directories exist and set permissions
RUN mkdir -p /var/www/storage /var/www/bootstrap/cache && \
    chown -R www-data:www-data /var/www && \
    chmod -R 775 /var/www/storage /var/www/bootstrap/cache

# Expose PHP-FPM port
EXPOSE 9000

# Start PHP-FPM server
CMD ["php-fpm"]
