# Aurelle — production images. Built by docker-compose.yml (targets "app" and "web").
# Base images are pinned to exact versions; PHP and npm packages come from composer.lock and
# package-lock.json, so every build installs the same versions.

# 1. Front-end: the theme and admin CSS/JS (Vite).
FROM node:22.12-alpine3.20 AS assets
WORKDIR /build
COPY package.json package-lock.json vite.config.js ./
RUN npm ci --no-audit --no-fund
COPY resources ./resources
COPY public ./public
RUN npm run build

# 2. The application: PHP-FPM with the extensions the shop needs.
FROM php:8.3.14-fpm-alpine3.20 AS app
RUN apk add --no-cache libzip-dev libpng-dev libjpeg-turbo-dev libwebp-dev freetype-dev icu-dev oniguruma-dev mysql-client \
    && docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install pdo_mysql gd zip intl opcache bcmath exif
COPY --from=composer:2.8.4 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html

# PHP packages first (cached until composer.lock changes), then the code.
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --no-autoloader --no-interaction --prefer-dist
COPY . .
COPY --from=assets /build/public/build ./public/build
RUN composer dump-autoload --no-dev --optimize --no-scripts \
    && rm -rf public/storage && ln -s ../storage/app/public public/storage \
    && mkdir -p storage/app/public storage/app/private 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 /usr/local/bin/entrypoint
RUN chmod +x /usr/local/bin/entrypoint
ENTRYPOINT ["entrypoint"]
EXPOSE 9000
CMD ["php-fpm"]

# 3. The web server: nginx with the public files (uploads come from the shared storage volume).
FROM nginx:1.27.3-alpine AS web
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=app /var/www/html/public /var/www/html/public
