FROM node:20-alpine AS node-builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --ignore-scripts
COPY . .
RUN npm run build:icons && npm run build

FROM composer:2.8 AS php-builder
WORKDIR /app
COPY composer*.json ./
RUN composer install \
    --no-dev \
    --no-interaction \
    --no-plugins \
    --no-scripts \
    --prefer-dist
COPY . .
RUN composer dump-autoload --no-dev --optimize --no-scripts

FROM php:8.4-fpm-alpine AS runtime
WORKDIR /var/www/html
RUN apk add --no-cache \
    libpng-dev \
    libjpeg-turbo-dev \
    freetype-dev \
    zip \
    libzip-dev \
    unzip \
    git \
    oniguruma-dev \
    $PHPIZE_DEPS \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install pdo pdo_mysql mbstring zip exif pcntl gd opcache

COPY docker/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

COPY --from=php-builder /app /var/www/html
COPY --from=node-builder /app/public/build /var/www/html/public/build

RUN rm -rf /var/www/html/docker

RUN ln -sf ../../storage/app/public /var/www/html/public/storage

RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache \
    && chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache

USER www-data

EXPOSE 9000
CMD ["php-fpm"]

