# ANBT Backend - PHP-FPM 8.2 production image.
#
# Phase 2 (repository hygiene & container hardening) changes versus the previous
# single-stage image:
#   1. Multi-stage build: Composer and the PHPIZE toolchain never reach the
#      runtime layer, so the shipped image has no compilers in it.
#   2. Runs as the unprivileged `www-data` user instead of root.
#   3. Declares a real HEALTHCHECK that talks to the FPM /ping endpoint, so
#      orchestrators can tell "process alive" from "pool able to serve".
#   4. Production php.ini and an FPM pool config are baked in; opcache runs with
#      validate_timestamps=0 (code is immutable inside the image).
#   5. Dependency layer is keyed on composer.json/composer.lock only, so editing
#      source no longer re-resolves dependencies.
#
# Build:  docker build -t anbt-backend:local ./backend
# Verify: docker run --rm anbt-backend:local id           # must NOT be uid 0

# =============================================================================
# Stage 1 - dependencies
# =============================================================================
FROM composer:2.7 AS deps

WORKDIR /app

# Only the dependency manifests, so this layer is cached until they change.
COPY composer.json composer.lock ./

# --no-scripts: no application code is present yet, so post-install hooks
# would run against an empty tree.
RUN composer install \
        --no-dev \
        --no-scripts \
        --no-interaction \
        --no-progress \
        --prefer-dist \
        --optimize-autoloader

# =============================================================================
# Stage 2 - runtime
# =============================================================================
FROM php:8.2-fpm-alpine AS runtime

# fcgi provides cgi-fcgi, used by the healthcheck below.
# mysql-client is kept for migration/ops commands run inside the container.
RUN apk add --no-cache \
        fcgi \
        mysql-client \
        libpng \
        libjpeg-turbo \
        freetype \
        libzip \
        icu-libs \
        oniguruma

# Build-only dependencies live in a single layer that is deleted again, so the
# compilers never appear in the final image.
RUN set -eux; \
    apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        libpng-dev \
        libjpeg-turbo-dev \
        freetype-dev \
        libzip-dev \
        icu-dev \
        oniguruma-dev; \
    docker-php-ext-configure gd --with-freetype --with-jpeg; \
    docker-php-ext-install -j"$(nproc)" \
        pdo_mysql \
        gd \
        zip \
        opcache \
        bcmath \
        pcntl \
        intl; \
    pecl install redis; \
    docker-php-ext-enable redis; \
    apk del .build-deps; \
    rm -rf /tmp/pear

# Production runtime configuration.
COPY docker/php/php-production.ini /usr/local/etc/php/conf.d/zz-production.ini
COPY docker/php/fpm-pool.conf      /usr/local/etc/php-fpm.d/zz-app.conf
COPY docker/php/healthcheck.sh     /usr/local/bin/fpm-healthcheck
RUN chmod +x /usr/local/bin/fpm-healthcheck

WORKDIR /var/www/html

# Vendor tree from the builder stage - the runtime image has no Composer.
COPY --from=deps --chown=www-data:www-data /app/vendor ./vendor

# Application source. .dockerignore keeps tests, docs, vendor and secrets out.
COPY --chown=www-data:www-data . .

# storage/ is the only writable path; everything else stays read-only to the
# application user. Run with `--read-only --tmpfs /tmp` for extra containment.
RUN set -eux; \
    mkdir -p storage/logs storage/cache; \
    chown -R www-data:www-data storage; \
    chmod -R 775 storage

# Drop privileges. Everything below this line runs unprivileged.
USER www-data

EXPOSE 9000

# "Is the pool accepting FastCGI requests?" - strictly stronger than checking
# that PID 1 is alive. start-period covers opcache priming on cold start.
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
    CMD ["fpm-healthcheck"]

CMD ["php-fpm", "-F"]
