FROM composer:2 AS composer
WORKDIR /app
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader --no-scripts

FROM node:20-alpine AS node
WORKDIR /app
COPY package.json package-lock.json vite.config.js tailwind.config.js postcss.config.js ./
COPY resources/ ./resources/
RUN npm ci && npm run build

FROM php:8.2-fpm-alpine
WORKDIR /var/www/html

RUN docker-php-ext-install -j$(nproc) pdo_mysql

RUN apk add --no-cache mariadb-client

COPY php.ini /usr/local/etc/php/conf.d/99-overrides.ini
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

COPY --from=composer /app/vendor ./vendor
COPY --from=node /app/public/build ./public/build

COPY --chown=www-data:www-data . .

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

USER www-data

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
