# syntax=docker/dockerfile:1
#
# Risk Locator App – production image (PHP 8.3 + Apache).
# Used by docker-compose.yml for the web app, the queue worker and the
# scheduler. The .env file is NOT in the image: it is mounted at runtime.

# --- 1. Front-end assets (Vite) ------------------------------------------------------------------
FROM node:22-bookworm-slim AS assets
WORKDIR /app
COPY package.json package-lock.json .npmrc ./
RUN npm ci --no-audit --no-fund
COPY vite.config.js ./
COPY resources ./resources
COPY public ./public
RUN npm run build

# --- 2. Application ------------------------------------------------------------------------------
FROM php:8.3-apache AS app

RUN apt-get update \
    && apt-get install -y --no-install-recommends libzip-dev libpng-dev libjpeg62-turbo-dev libfreetype6-dev libicu-dev unzip \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" pdo_mysql zip gd intl bcmath opcache \
    && a2enmod rewrite headers \
    && rm -rf /var/lib/apt/lists/*

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
COPY docker/php/php.ini /usr/local/etc/php/conf.d/zz-risk-locator.ini
COPY docker/apache/vhost.conf /etc/apache2/sites-available/000-default.conf
RUN echo "ServerTokens Prod\nServerSignature Off" > /etc/apache2/conf-available/zz-security.conf && a2enconf zz-security

WORKDIR /var/www/html

# Dependencies first (cached while composer.lock does not change).
COPY composer.json composer.lock ./
RUN composer install --no-dev --prefer-dist --no-interaction --no-progress --no-scripts --no-autoloader

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

RUN composer dump-autoload --optimize --classmap-authoritative --no-dev \
    && php artisan package:discover --ansi \
    && mkdir -p storage/framework/cache/data storage/framework/sessions storage/framework/views storage/logs bootstrap/cache \
    && chown -R www-data:www-data storage bootstrap/cache

EXPOSE 80
CMD ["apache2-foreground"]
