FROM php:8.3-fpm

RUN apt-get update && apt-get install -y \
    git curl unzip libpng-dev libjpeg-dev libwebp-dev libfreetype6-dev \
    libonig-dev libxml2-dev libzip-dev \
    && docker-php-ext-configure gd --with-jpeg --with-webp --with-freetype \
    && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip opcache \
    && rm -rf /var/lib/apt/lists/*

# OPcache was never enabled — PHP was re-parsing every single .php file on
# every request (autoloader, config, every class). This alone is often the
# single biggest performance lever for a Laravel app. validate_timestamps=1
# keeps it dev-friendly (file edits are picked up automatically); for a real
# production deploy, override with validate_timestamps=0 for max speed and
# reload php-fpm on each deploy instead.
RUN { \
        echo 'opcache.enable=1'; \
        echo 'opcache.memory_consumption=192'; \
        echo 'opcache.interned_strings_buffer=16'; \
        echo 'opcache.max_accelerated_files=20000'; \
        echo 'opcache.validate_timestamps=1'; \
        echo 'opcache.revalidate_freq=0'; \
        echo 'opcache.save_comments=1'; \
    } > /usr/local/etc/php/conf.d/opcache-custom.ini

# PHP's own defaults (~2MB upload / 8MB post) are smaller than the 5MB
# photo/gallery uploads this app validates against — without this, uploads
# fail silently before Laravel's own validation ever runs.
RUN { \
        echo 'upload_max_filesize=10M'; \
        echo 'post_max_size=12M'; \
        echo 'max_file_uploads=20'; \
        echo 'memory_limit=256M'; \
        echo 'expose_php=Off'; \
    } > /usr/local/etc/php/conf.d/uploads-custom.ini

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

WORKDIR /var/www/html

COPY . .

RUN composer install --optimize-autoloader --no-dev --no-interaction || true

RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

EXPOSE 9000

# The chown above only survives until Windows' bind mount overlays the whole
# project directory at `docker compose up` time (its own permissions win),
# so storage/bootstrap-cache end up unwritable again despite the build-time
# chown. Fixing it here, right before php-fpm starts, makes it durable
# across every container start regardless of host OS permission quirks.
CMD ["sh", "-c", "chmod -R 777 storage bootstrap/cache && php-fpm"]
