FROM php:8.4-fpm-alpine

# Application root (same default as the apache image)
ENV APP_ROOT="/var/www/html"

ENV TZ="Europe/Prague" \
    PHP_INI_FILE="$PHP_INI_DIR/php.ini" \
    COMPOSER_ALLOW_SUPERUSER=1 \
    PHP_OPCACHE_VALIDATE_TIMESTAMPS="1" \
    PHP_OPCACHE_MAX_ACCELERATED_FILES="10000" \
    PHP_OPCACHE_MEMORY_CONSUMPTION="192" \
    PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"

RUN apk add --no-cache \
    tzdata \
    bash \
    ca-certificates \
    libpq \
    libzip \
    imagemagick \
    imagemagick-dev \
    librsvg \
    freetype \
    libjpeg-turbo \
    libpng \
    icu \
    oniguruma \
    libxml2 \
    libwebp \
    git \
    nano \
    zip \
    unzip \
    nodejs \
    npm \
    supervisor \
    mysql-client \
    postgresql-client

RUN apk add --no-cache --virtual .build-deps \
    $PHPIZE_DEPS \
    postgresql-dev \
    libzip-dev \
    freetype-dev \
    libjpeg-turbo-dev \
    libwebp-dev \
    libpng-dev \
    icu-dev \
    libxml2-dev \
    linux-headers

RUN docker-php-ext-configure \
    gd --with-freetype --with-jpeg --with-webp \
    && docker-php-ext-install -j$(nproc) \
    pdo_mysql \
    pdo_pgsql \
    pgsql \
    zip \
    bcmath \
    soap \
    gd \
    intl \
    opcache \
    sockets \
    pcntl \
    calendar

RUN pecl install imagick \
 && docker-php-ext-enable imagick

RUN apk del .build-deps

# Fail fast if the JPEG delegate did not make it into the image
RUN php -r 'if (!in_array("JPEG", Imagick::queryFormats())) { fwrite(STDERR, "ERROR: imagick built without JPEG delegate\n"); exit(1); } echo "imagick JPEG delegate OK\n";' && \
    php -r 'if (!function_exists("imagecreatefromjpeg")) { fwrite(STDERR, "ERROR: gd built without JPEG support\n"); exit(1); } echo "gd JPEG OK\n";'

# Composer
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer

# PHP
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_FILE" && \
    sed -i "s|memory_limit = 128M|memory_limit = 256M |g" "$PHP_INI_FILE" && \
    sed -i "s|upload_max_filesize = 2M|upload_max_filesize = 100M |g" "$PHP_INI_FILE" && \
    sed -i "s|post_max_size = 8M|post_max_size = 100M |g" "$PHP_INI_FILE" && \
    printf '[Date]\ndate.timezone="%s"\n' "$TZ" > "$PHP_INI_DIR/conf.d/tzone.ini"

#OPCACHE
RUN { \
    echo "[opcache]"; \
    echo "opcache.enable=1"; \
    echo "opcache.revalidate_freq=0"; \
    echo "opcache.validate_timestamps=${PHP_OPCACHE_VALIDATE_TIMESTAMPS}"; \
    echo "opcache.max_accelerated_files=${PHP_OPCACHE_MAX_ACCELERATED_FILES}"; \
    echo "opcache.memory_consumption=${PHP_OPCACHE_MEMORY_CONSUMPTION}"; \
    echo "opcache.max_wasted_percentage=${PHP_OPCACHE_MAX_WASTED_PERCENTAGE}"; \
    echo "opcache.interned_strings_buffer=16"; \
    echo "opcache.fast_shutdown=1"; \
} > "$PHP_INI_DIR/conf.d/opcache.ini"

# Supervisor (queue workers + scheduler, started from entrypoint.d/90-supervisord.sh)
RUN mkdir -p /var/log/supervisor
COPY ./supervisor.d/ /etc/supervisor/conf.d/

# Entrypoint scripts (run in order from the stock entrypoint)
COPY --chmod=755 ./entrypoint.d/ /etc/entrypoint.d/
COPY --chmod=755 ./entrypoint-runner.sh /usr/local/bin/entrypoint-runner.sh

# Persistent .env storage (see entrypoint.d/20-env.sh)
RUN mkdir -p /config
VOLUME /config
RUN sed -i 's#^exec #entrypoint-runner.sh\n\nexec #' /usr/local/bin/docker-php-entrypoint

WORKDIR ${APP_ROOT}
