FROM node:16 AS assets

WORKDIR /app

# Install Elixir/Gulp dependencies and build the front-end assets.
COPY package.json package-lock.json ./
RUN npm install

COPY gulpfile.js .
COPY webpack.mix.js .
COPY resources resources
COPY public public

RUN npm run prod

FROM php:7.4-fpm

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    locales \
    git \
    unzip \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libicu-dev \
    libsqlite3-dev \
    curl \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
    bcmath \
    exif \
    gd \
    intl \
    mbstring \
    mysqli \
    pcntl \
    pdo_mysql \
    pdo_sqlite \
    zip \
    && docker-php-source delete \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

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

WORKDIR /var/www/html

# COPY composer.json composer.lock ./
# RUN composer install --no-dev --no-interaction --prefer-dist

# Copy the application (assets, config, bootstrap, etc.).
COPY . .

RUN mkdir -p storage bootstrap/cache && chown -R www-data:www-data storage bootstrap/cache

# Overlay the compiled assets so Docker always ships the production files.
COPY --from=assets /app/public /var/www/html/public

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

EXPOSE 9000

CMD ["php-fpm"]
