# syntax=docker/dockerfile:1

# =============================================================================
# EFET backend (Laravel 8) — multi-stage image
#   base        -> php:8.1-apache + extensions (shared by dev & production)
#   dev         -> local development target (code is bind-mounted at runtime)
#   production  -> self-contained image for the server (Portainer runs this)
#
# NOTE on assets: public/js/app.js and public/css/app.css are COMMITTED to the
# repo and shipped as-is, exactly like the current (non-docker) deploy does.
# The legacy toolchain (laravel-mix 4 / webpack 4) cannot run on Node 17+
# (ERR_OSSL_EVP_UNSUPPORTED), so it is deliberately not built inside the image.
# Recompile locally with `yarn prod` and commit the result when styles change.
# =============================================================================

# -----------------------------------------------------------------------------
# Stage 1 — PHP/Apache base (system libs + extensions + Composer)
# -----------------------------------------------------------------------------
FROM php:8.1-apache AS base

# Extensions: the set the app has always needed (gd here also gets jpeg+freetype,
# which the old CI image lacked), plus exif for image orientation.
RUN apt-get update && apt-get install -y --no-install-recommends \
        git curl unzip gettext-base default-mysql-client \
        libpng-dev libjpeg-dev libfreetype6-dev libzip-dev libxslt1-dev libicu-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" pdo_mysql gd zip xsl bcmath intl exif \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Apache: serve Laravel's public/ dir and enable rewrite for the front controller.
RUN a2enmod rewrite headers remoteip
COPY docker/apache/000-default.conf /etc/apache2/sites-available/000-default.conf
COPY docker/php/php.ini /usr/local/etc/php/conf.d/zz-app.ini

# Composer (pinned to 2.2.x to match the Lando setup).
COPY --from=composer:2.2 /usr/bin/composer /usr/bin/composer

WORKDIR /var/www/html

COPY docker/entrypoint.sh /usr/local/bin/entrypoint
RUN chmod +x /usr/local/bin/entrypoint
ENTRYPOINT ["entrypoint"]
CMD ["apache2-foreground"]

# -----------------------------------------------------------------------------
# Stage 2a — dev (no code baked in; bind-mounted via docker-compose.yml)
# -----------------------------------------------------------------------------
FROM base AS dev
ENV APP_ENV=local
# Entrypoint runs `composer install` on first boot if vendor/ is missing.

# -----------------------------------------------------------------------------
# Stage 2b — production (self-contained, used by Portainer)
# -----------------------------------------------------------------------------
FROM base AS production
ENV APP_ENV=staging

# Names the stack this image belongs to, so the stack's cleanup service can
# prune its own superseded builds without touching any other stack on the
# server — including the sibling EFET ones. Passed as a build arg by the
# compose file, so the same repo can back more than one stack.
ARG STACK_LABEL=efet-public
LABEL com.efet.stack=$STACK_LABEL

COPY . /var/www/html

# A throwaway .env lets Composer's post-autoload-dump (package:discover) boot the
# app during build. The real .env is generated at container start by the entrypoint.
RUN cp .env.example .env \
    && composer install --no-dev --optimize-autoloader --no-interaction --prefer-dist \
    && rm -f .env

# Writable dirs must exist in the image so the named volumes inherit www-data.
RUN 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
