# syntax=docker/dockerfile:1.7
#
# ALM Talent - API image (Laravel on PHP-FPM). DRAFT for Stage 6 (prep/deploy).
#
# One image, four roles. The entrypoint picks the role from the command:
#   api        php-fpm                              (default CMD; Caddy talks FastCGI to :9000)
#   worker     php artisan queue:work ...            (compose services "worker" and "worker-media")
#   scheduler  php artisan schedule:work             (compose service "scheduler")
#   one-off    any other command, e.g. php artisan migrate --force (run by deploy.sh)
#
# Platform: linux/amd64, built on the server (Hostinger KVM 2, D-057; DEPLOY.md §1.3).
# Build context = repository root:
#   docker build -f deploy/docker/api/Dockerfile -t alm-local/alm-talent-api:<sha> --build-arg ALM_GIT_SHA=<sha> .
#
# Pin the base images by digest at Stage 6 (PLAN_TECH_NOTES T20): pass them as build args.
# PHP 8.3 matches the local build (D-042). Change PHP_IMAGE only together with the lead's PHP decision.

ARG PHP_IMAGE=php:8.3-fpm-alpine
ARG COMPOSER_IMAGE=composer:2

# --------------------------------------------------------------------------------------------------
# 1. Composer dependencies (plain PHP vendor code, copied into the runtime stage).
# --------------------------------------------------------------------------------------------------
FROM ${COMPOSER_IMAGE} AS vendor
WORKDIR /app
COPY api/composer.json api/composer.lock ./
# --ignore-platform-reqs: the composer image lacks pgsql/gd/intl...; the lock file was resolved for PHP 8.3
# with those extensions, and the runtime stage below provides them.
RUN --mount=type=cache,target=/tmp/composer-cache \
    COMPOSER_CACHE_DIR=/tmp/composer-cache \
    composer install --no-dev --no-interaction --no-progress --prefer-dist \
        --no-scripts --no-autoloader --ignore-platform-reqs
COPY api/ ./
# Scripts are skipped here; "artisan package:discover" runs in the runtime stage, which has the extensions.
RUN composer dump-autoload --no-dev --optimize --no-scripts --no-interaction

# --------------------------------------------------------------------------------------------------
# 2. Runtime
# --------------------------------------------------------------------------------------------------
FROM ${PHP_IMAGE} AS runtime

ARG PECL_REDIS_VERSION=6.2.0
ARG PECL_IMAGICK_VERSION=3.8.0
# Imagick keeps the ICC colour profile when EXIF/GPS is stripped (BUILD_PLAN M2.3) and reads HEIC
# (PLAN_TECH_NOTES T19 fallback). Set to 0 to build without it (GD only).
ARG WITH_IMAGICK=1

# Runtime libraries (kept) ------------------------------------------------------------------------
# NOTE: Alpine splits ImageMagick coders into sub-packages; re-check the names on the first server build.
RUN set -eux; \
    apk add --no-cache \
        bash fcgi su-exec tzdata procps \
        icu-libs libzip libpng libjpeg-turbo libwebp freetype libpq gmp; \
    if [ "$WITH_IMAGICK" = "1" ]; then \
        apk add --no-cache imagemagick imagemagick-jpeg imagemagick-webp imagemagick-heic libgomp; \
    fi

# PHP extensions (build deps removed afterwards) --------------------------------------------------
# Already in the official image: ctype curl dom fileinfo filter iconv mbstring openssl pdo session
# simplexml sodium tokenizer xml xmlreader xmlwriter zlib.
# pdo_mysql is included so the image also runs against MySQL if the server choice needs it (D-040).
# pcntl + redis let production move to Valkey/Horizon later without a new image (BUILD_PLAN §3.3).
RUN set -eux; \
    apk add --no-cache --virtual .build-deps $PHPIZE_DEPS linux-headers \
        icu-dev libzip-dev libpng-dev libjpeg-turbo-dev libwebp-dev freetype-dev postgresql-dev gmp-dev; \
    if [ "$WITH_IMAGICK" = "1" ]; then apk add --no-cache --virtual .imagick-build-deps imagemagick-dev; fi; \
    docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp; \
    docker-php-ext-install -j"$(nproc)" bcmath exif gd gmp intl opcache pcntl pdo_mysql pdo_pgsql pgsql zip; \
    pecl install "redis-${PECL_REDIS_VERSION}"; \
    docker-php-ext-enable redis; \
    if [ "$WITH_IMAGICK" = "1" ]; then \
        pecl install "imagick-${PECL_IMAGICK_VERSION}"; \
        docker-php-ext-enable imagick; \
        apk del --no-network .imagick-build-deps; \
    fi; \
    apk del --no-network .build-deps; \
    rm -rf /tmp/pear ~/.pearrc; \
    php -m

# PHP + FPM configuration -------------------------------------------------------------------------
RUN cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY deploy/docker/api/php.ini      /usr/local/etc/php/conf.d/zz-alm.ini
COPY deploy/docker/api/php-fpm.conf /usr/local/etc/php-fpm.d/zzz-alm.conf
COPY deploy/docker/api/entrypoint.sh /usr/local/bin/alm-entrypoint
RUN chmod 0755 /usr/local/bin/alm-entrypoint

# Application ---------------------------------------------------------------------------------------
WORKDIR /var/www/html
# Code stays owned by root (read-only for the FPM workers); only storage/ and bootstrap/cache are writable.
COPY --from=vendor /app/ ./
RUN set -eux; \
    rm -f .env; \
    mkdir -p storage/app/public storage/app/private storage/framework/cache/data \
             storage/framework/sessions storage/framework/views storage/logs bootstrap/cache; \
    ln -sfn ../storage/app/public public/storage; \
    chown -R www-data:www-data storage bootstrap/cache; \
    # APP_ENV=build: without an env the app boots as "production", where the ALM boot guard (ProductionGuard)
    # refuses the default stand-in drivers and would fail the image build.
    APP_ENV=build su-exec www-data php artisan package:discover --ansi

# Build metadata (shown by the AGPL "Source code" page and by deploy.sh) ---------------------------
ARG ALM_GIT_SHA=unknown
ARG ALM_VERSION=0.0.0-dev
ENV ALM_BUILD_SHA=${ALM_GIT_SHA} \
    ALM_BUILD_VERSION=${ALM_VERSION} \
    TZ=UTC
LABEL org.opencontainers.image.title="alm-talent-api" \
      org.opencontainers.image.description="ALM Talent API (Laravel, based on OpnForm)" \
      org.opencontainers.image.licenses="AGPL-3.0-only" \
      org.opencontainers.image.revision="${ALM_GIT_SHA}" \
      org.opencontainers.image.version="${ALM_VERSION}" \
      org.opencontainers.image.source="https://github.com/Almano-2/ALM-Talent-Platform"

EXPOSE 9000
STOPSIGNAL SIGQUIT
ENTRYPOINT ["/usr/local/bin/alm-entrypoint"]
CMD ["php-fpm"]
