# Use an Ubuntu-based PHP 7.2 FPM image
FROM php:7.2-fpm

# Set environment variables
ENV PHP_OPCACHE_ENABLE=1

# Install required dependencies
RUN apt-get update && apt-get install -y \
    bash \
    git \
    npm \
    curl \
    unzip \
    libzip-dev \
    mariadb-client \
    supervisor \
    libicu-dev \
    libcurl4-openssl-dev \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libxml2-dev \
    && docker-php-ext-configure intl \
    && docker-php-ext-configure curl --with-curl \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install \
        intl \
        curl \
        gd \
        mbstring \
        zip \
        pdo_mysql \
        xml \
        opcache \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Set working directory
WORKDIR /app/concurso

# Copy Laravel application
COPY ./ /app/concurso

# Ensure correct permissions before running Composer
RUN chown -R www-data:www-data /app/concurso \
    && chmod -R 775 /app/concurso/storage /app/concurso/bootstrap/cache

# Install Composer dependencies inside the container
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer 

#RUN composer install --no-interaction --no-dev --optimize-autoloader --prefer-dist || true 
RUN composer install --no-interaction

# Ensure `vendor/` directory exists and set correct permissions
RUN mkdir -p /app/concurso/vendor \
    && chown -R www-data:www-data /app/concurso/vendor

# Fix permissions for Laravel storage and bootstrap/cache
RUN chown -R www-data:www-data /app/concurso/storage /app/concurso/bootstrap/cache \
    && chmod -R 775 /app/concurso/storage /app/concurso/bootstrap/cache


# Ensure writable cache for Laravel
RUN mkdir -p /app/concurso/storage/framework/{sessions,views,cache} \
    && chmod -R 775 /app/concurso/storage \
    && chown -R www-data:www-data /app/concurso/storage

# Set Laravel permissions for PHP-FPM
RUN chown -R www-data:www-data /app/concurso

RUN php artisan migrate


# Expose PHP-FPM port
EXPOSE 9000

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