FROM php:8.4-fpm-alpine

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

ENV TZ="Europe/Prague" \
    COMPOSER_ALLOW_SUPERUSER=1 \
    PHP_OPCACHE_ENABLE=1 \
    PHP_OPCACHE_VALIDATE_TIMESTAMPS=0 \
    PHP_OPCACHE_REVALIDATE_FREQ=0 \
    PHP_OPCACHE_MEMORY_CONSUMPTION=192 \
    PHP_OPCACHE_MAX_ACCELERATED_FILES=20000 \
    PHP_OPCACHE_MAX_WASTED_PERCENTAGE=10 \
    PHP_OPCACHE_INTERNED_STRINGS_BUFFER=16 \
    APP_ENV=production \
    APP_DEBUG=false

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

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" \
    && echo "date.timezone=${TZ}" > "$PHP_INI_DIR/conf.d/99-timezone.ini"

RUN { \
    echo "memory_limit=256M"; \
    echo "upload_max_filesize=100M"; \
    echo "post_max_size=100M"; \
    echo "realpath_cache_size=4096K"; \
    echo "realpath_cache_ttl=600"; \
} > "$PHP_INI_DIR/conf.d/99-laravel.ini"

RUN { \
    echo "[opcache]"; \
    echo "opcache.enable=${PHP_OPCACHE_ENABLE}"; \
    echo "opcache.enable_cli=0"; \
    echo "opcache.validate_timestamps=${PHP_OPCACHE_VALIDATE_TIMESTAMPS}"; \
    echo "opcache.revalidate_freq=${PHP_OPCACHE_REVALIDATE_FREQ}"; \
    echo "opcache.memory_consumption=${PHP_OPCACHE_MEMORY_CONSUMPTION}"; \
    echo "opcache.interned_strings_buffer=${PHP_OPCACHE_INTERNED_STRINGS_BUFFER}"; \
    echo "opcache.max_accelerated_files=${PHP_OPCACHE_MAX_ACCELERATED_FILES}"; \
    echo "opcache.max_wasted_percentage=${PHP_OPCACHE_MAX_WASTED_PERCENTAGE}"; \
    echo "opcache.save_comments=1"; \
} > "$PHP_INI_DIR/conf.d/10-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/
RUN sed -i 's#^exec #for f in /etc/entrypoint.d/*.sh; do if [ -x "$f" ]; then "$f" || { echo "entrypoint script failed: $f" >\&2; exit 1; }; fi; done\n\nexec #' /usr/local/bin/docker-php-entrypoint

WORKDIR ${APP_ROOT}
