FROM php:8.5.4-fpm-alpine AS builder

ARG http_proxy

ENV http_proxy=${http_proxy} \
    https_proxy=${http_proxy} \
    HTTP_PROXY=${http_proxy} \
    HTTPS_PROXY=${http_proxy}

# Installation des packages nécessaires à la compilation des extensions php.
RUN apk update && apk upgrade --no-cache && apk add --no-cache \
    unzip \
    freetype-dev jpeg-dev libpng-dev libwebp-dev libxpm-dev \
    zlib-dev icu-dev libxslt-dev libzip-dev \
    imagemagick-dev \
    libmemcached-dev cyrus-sasl-dev \
    oniguruma-dev gettext-dev gmp-dev \
    linux-headers \
    autoconf libtool pkgconfig alpine-sdk

# Installation des extensions php natives.
RUN docker-php-ext-configure gd --enable-gd --with-webp --with-jpeg --with-xpm --with-freetype \
  && docker-php-ext-configure zip --with-zip \
  && docker-php-ext-install -j"$(nproc)" \
    exif \
    gd \
    gettext \
    intl \
    mysqli \
    pdo_mysql \
    xsl \
    zip \
    bcmath \
    gmp

# PECL maintenant déprécié, on installe directement les extensions souhaitée depuis github.
RUN \
    curl -fsSL https://github.com/Imagick/imagick/archive/refs/tags/3.8.1.tar.gz -o /tmp/imagick.tar.gz \
    && tar -xzf /tmp/imagick.tar.gz -C /tmp \
    && cd /tmp/imagick-3.8.1 \
    && phpize && ./configure && make -j"$(nproc)" && make install \
    && rm -rf /tmp/imagick* \
    \
    && curl -fsSL https://github.com/php-memcached-dev/php-memcached/archive/refs/tags/v3.4.0.tar.gz -o /tmp/memcached.tar.gz \
    && tar -xzf /tmp/memcached.tar.gz -C /tmp \
    && cd /tmp/php-memcached-3.4.0 \
    && phpize && ./configure && make -j"$(nproc)" && make install \
    && rm -rf /tmp/memcached* \
    \
    && curl -fsSL https://github.com/phpredis/phpredis/archive/refs/tags/6.3.0.tar.gz -o /tmp/redis.tar.gz \
    && tar -xzf /tmp/redis.tar.gz -C /tmp \
    && cd /tmp/phpredis-6.3.0 \
    && phpize && ./configure && make -j"$(nproc)" && make install \
    && rm -rf /tmp/redis* \
    \
    && curl -fsSL https://github.com/xdebug/xdebug/archive/refs/tags/3.5.1.tar.gz -o /tmp/xdebug.tar.gz \
    && tar -xzf /tmp/xdebug.tar.gz -C /tmp \
    && cd /tmp/xdebug-3.5.1 \
    && phpize && ./configure && make -j"$(nproc)" && make install \
    && rm -rf /tmp/xdebug* \
    \
    && curl -fsSL https://github.com/krakjoe/pcov/archive/refs/tags/v1.0.12.tar.gz -o /tmp/pcov.tar.gz \
    \
    && tar -xzf /tmp/pcov.tar.gz -C /tmp \
    && cd /tmp/pcov-1.0.12 \
    && phpize && ./configure && make -j"$(nproc)" && make install \
    && rm -rf /tmp/pcov*

# Installation composer + Installation bun (sous alpine).
RUN curl -fsSL https://getcomposer.org/installer -o /tmp/composer-installer \
    && php /tmp/composer-installer --install-dir=/usr/local/bin --filename=composer --version=2.9.7 \
    \
    && curl -fsSL -o /tmp/bun.zip "https://github.com/oven-sh/bun/releases/download/bun-v1.2.10/bun-linux-x64-musl.zip" \
    && unzip -q /tmp/bun.zip -d /tmp/bun-extracted \
    && mv /tmp/bun-extracted/bun-linux-x64-musl/bun /usr/local/bin/bun \
    && chmod +x /usr/local/bin/bun \
    && rm -rf /tmp/bun.zip /tmp/bun-extracted

FROM php:8.5.4-fpm-alpine AS base

ARG http_proxy

ENV http_proxy=${http_proxy} \
    https_proxy=${http_proxy} \
    HTTP_PROXY=${http_proxy} \
    HTTPS_PROXY=${http_proxy} \
    LC_ALL=fr_FR.UTF-8 \
    LANG=fr_FR.UTF-8 \
    LANGUAGE=fr_FR.UTF-8:fr \
    SHELL=/bin/zsh \
    XDEBUG_MODE=develop,debug \
    XDEBUG_CONFIG="client_host=host.docker.internal client_port=9003"

# Installation des packages runtime + utilitaires + outils QA.
RUN apk update && apk upgrade --no-cache && apk add --no-cache \
    bash \
    ca-certificates curl \
    git unzip zip nano vim zsh htop \
    apache2 apache2-proxy apache2-brotli \
    nodejs npm \
    freetype libjpeg libpng libwebp libxpm \
    icu-libs libxslt libzip \
    imagemagick \
    libcurl libmemcached \
    musl-locales musl-locales-lang \
    oniguruma \
    shadow man-db \
    gmp gettext-libs \
    zsh-vcs

# Installation ohmyzsh + plugin + theme.
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended \
    && git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions \
    && sed -i 's/plugins=(git)/plugins=(git laravel zsh-autosuggestions)/g' ~/.zshrc \
    && sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="agnoster"/g' ~/.zshrc \
    && chsh -s /bin/zsh

# Copie des extensions PHP compilées + conf PHP + Composer + Bun depuis le stage builder.
COPY --from=builder /usr/local/lib/php/extensions/ /usr/local/lib/php/extensions/
COPY --from=builder /usr/local/etc/php/conf.d/ /usr/local/etc/php/conf.d/
COPY --from=builder /usr/local/bin/composer /usr/local/bin/composer
COPY --from=builder /usr/local/bin/bun /usr/local/bin/bun

# Active les extensions PECL copiées + supprime xdebug.ini auto-généré car on veut utiliser notre propre fichier de config.
RUN docker-php-ext-enable imagick memcached redis xdebug pcov \
    && rm -f /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

# Copie des fichiers de configuration + Entrypoint
COPY conf/php-fpm.conf         /usr/local/etc/php-fpm.d/www.conf
COPY conf/php.ini              /usr/local/etc/php/conf.d/app.ini
COPY conf/xdebug.ini           /usr/local/etc/php/conf.d/xdebug.ini
COPY conf/vhost.conf           /etc/apache2/conf.d/vhost.conf
COPY conf/common-apache.conf   /etc/apache2/conf.d/z-app.conf
COPY conf/common-security.conf /etc/apache2/conf.d/security.conf
COPY --chmod=755 conf/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

# Configuration des modules apache sans duplication (par défaut dans alpine c'est commenté, on décommente juste ce dont on a besoin)
RUN sed -i \
    -e 's/#LoadModule proxy_module/LoadModule proxy_module/' \
    -e 's/#LoadModule proxy_fcgi_module/LoadModule proxy_fcgi_module/' \
    -e 's/#LoadModule rewrite_module/LoadModule rewrite_module/' \
    -e 's/#LoadModule remoteip_module/LoadModule remoteip_module/' \
    -e 's/#LoadModule headers_module/LoadModule headers_module/' \
    -e 's/#LoadModule expires_module/LoadModule expires_module/' \
    -e 's/#LoadModule deflate_module/LoadModule deflate_module/' \
    -e 's/#LoadModule mime_module/LoadModule mime_module/' \
    -e 's/#LoadModule mpm_event_module/LoadModule mpm_event_module/' \
    -e 's/LoadModule mpm_prefork_module/#LoadModule mpm_prefork_module/' \
    -e 's/^[[:space:]]*#*[[:space:]]*LoadModule[[:space:]]\+ssl_module/#LoadModule ssl_module/' \
    -e 's/^[[:space:]]*Listen[[:space:]]\+443/#Listen 443/' \
    /etc/apache2/httpd.conf \
    && printf '\nPidFile /tmp/httpd.pid\n' >> /etc/apache2/httpd.conf \
    && if [ -f /etc/apache2/conf.d/ssl.conf ]; then \
         mv /etc/apache2/conf.d/ssl.conf \
            /etc/apache2/conf.d/ssl.conf.disabled; \
       fi \
    && find /etc/apache2 -type f \( -name '*.conf' -o -name 'httpd.conf' \) \
        -exec sed -i \
            -e 's!^[[:space:]]*CustomLog[[:space:]].*!CustomLog /proc/self/fd/1 combined!' \
            -e 's!^[[:space:]]*TransferLog[[:space:]].*!CustomLog /proc/self/fd/1 combined!' \
            -e 's!^[[:space:]]*ErrorLog[[:space:]].*!ErrorLog /proc/self/fd/2!' \
            -e 's/^[[:space:]]*#*[[:space:]]*LoadModule[[:space:]]\+lbmethod_heartbeat_module/#LoadModule lbmethod_heartbeat_module/' \
            -e 's/^[[:space:]]*#*[[:space:]]*LoadModule[[:space:]]\+lbmethod_/#&/' \
            -e 's/^[[:space:]]*#*[[:space:]]*LoadModule[[:space:]]\+proxy_balancer_module/#&/' \
            {} \; \
    && httpd -t

# Création des répertoires avec les bons droits.
RUN mkdir -p /app/tmp /app/tmp/sessions /app/logs /app/srcs \
    && chown -R www-data:www-data /app \
    && chmod 770 /app/tmp /app/logs

USER www-data

# Définit le répertoire de travail par défaut dans le conteneur.
WORKDIR /app/srcs

# Entrypoint custom pour démarrer php-fpm et apache (en executable seule).
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]