FROM dunglas/frankenphp:php8.4-alpine AS php

WORKDIR /app


RUN install-php-extensions \
    bcmath \
    pdo_pgsql \
    opcache \
    pcntl \
    zip \
    intl


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


COPY composer.json composer.lock ./

RUN composer install \
    --no-dev \
    --no-interaction \
    --prefer-dist \
    --optimize-autoloader \
    --no-scripts


COPY . .


RUN composer dump-autoload --optimize


FROM node:22-alpine AS frontend

WORKDIR /app

COPY package.json package-lock.json ./
RUN npm ci

COPY . .
COPY --from=php /app/vendor ./vendor

RUN npm run build


FROM php AS production

COPY --from=frontend /app/public/build ./public/build



RUN mkdir -p \
        storage/app/public \
        storage/framework/cache/data \
        storage/framework/sessions \
        storage/framework/views \
        storage/logs \
        bootstrap/cache \
    && chown -R www-data:www-data storage bootstrap/cache


COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh


EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]
