# syntax=docker/dockerfile:1

# ---------- Stage 1: Frontend assets (Vite + Tailwind) ----------
FROM node:20-alpine AS assets
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY resources ./resources
COPY vite.config.js ./
COPY public ./public
RUN npm run build

# ---------- Stage 2: PHP runtime (php-fpm + nginx) ----------
FROM serversideup/php:8.3-fpm-nginx AS app

# Install PHP extensions the app needs
USER root
RUN install-php-extensions \
        pdo_mysql \
        bcmath \
        gd \
        intl \
        zip \
        exif \
        pcntl \
        redis \
        opcache

WORKDIR /var/www/html

# Composer dependencies first (better layer caching)
COPY --chown=www-data:www-data composer.json composer.lock ./
RUN composer install \
        --no-dev \
        --no-scripts \
        --no-autoloader \
        --prefer-dist \
        --no-interaction

# Application source
COPY --chown=www-data:www-data . .

# Compiled frontend assets from stage 1
COPY --chown=www-data:www-data --from=assets /app/public/build ./public/build

# Optimize autoloader
RUN composer dump-autoload --optimize --no-dev --no-interaction

# Runtime init (migrate + cache) runs on container start
COPY --chown=www-data:www-data docker/entrypoint.d/ /etc/entrypoint.d/
RUN chmod +x /etc/entrypoint.d/*.sh

USER www-data
