ARG DRUPAL_VERSION=11.3.3

FROM drupal:${DRUPAL_VERSION}-php8.4-fpm-bookworm

ARG NODE_ENV=production
RUN apt-get update && apt-get install -y \
    git \
    vim \
    && rm -rf /var/lib/apt/lists/*

# Upload progress
RUN	set -eux; \
    git clone https://github.com/php/pecl-php-uploadprogress/ /usr/src/php/ext/uploadprogress/; \
    docker-php-ext-configure uploadprogress; \
    docker-php-ext-install uploadprogress; \
    rm -rf /usr/src/php/ext/uploadprogress;

# Install apcu.
RUN set -eux; \
    pecl install apcu; \
    docker-php-ext-enable apcu;

# Configure apcu (extension already loaded by docker-php-ext-enable).
RUN { \
    echo "apc.enable_cli=1"; \
    echo "apc.enable=1"; \
    echo "apc.shm_size=32M"; \
    } >> /usr/local/etc/php/conf.d/zz-apcu-custom.ini;

# Enable output buffering
RUN { \
    echo 'output_buffering = on'; \
    } >> /usr/local/etc/php/conf.d/zz-drupal-recommended.ini;

# Enable Xdebug in development environment.
RUN if [ "$NODE_ENV" = "development" ]; then \
    set -eux; \
    pecl install xdebug; \
    docker-php-ext-enable xdebug; \
fi;

# Configure Xdebug in development environment.
# Configure xdebug (extension already loaded by docker-php-ext-enable).
RUN if [ "$NODE_ENV" = "development" ]; then \
    { \
    echo 'xdebug.mode=debug'; \
    echo 'xdebug.start_with_request=yes'; \
    echo 'xdebug.client_host=host.docker.internal'; \
    echo 'xdebug.client_port=9000'; \
    echo 'xdebug.idekey=VSCODE'; \
    echo 'xdebug.log=/var/log/xdebug.log'; \
    } >> /usr/local/etc/php/conf.d/zz-xdebug-custom.ini; \
fi

# Install and enable opcache.
RUN set -eux; \
    docker-php-ext-install opcache; \
    docker-php-ext-enable opcache

# Configure opcache: dev (revalidate on) vs production (revalidate off).
RUN if [ "$NODE_ENV" = "development" ]; then \
    { \
    echo 'opcache.enable=1'; \
    echo 'opcache.enable_cli=1'; \
    echo 'opcache.memory_consumption=128'; \
    echo 'opcache.interned_strings_buffer=16'; \
    echo 'opcache.max_accelerated_files=10000'; \
    echo 'opcache.save_comments=1'; \
    echo 'opcache.validate_timestamps=1'; \
    echo 'opcache.revalidate_freq=0'; \
    echo 'opcache.fast_shutdown=1'; \
    } > /usr/local/etc/php/conf.d/zz-opcache-custom.ini; \
else \
    { \
    echo 'opcache.enable=1'; \
    echo 'opcache.enable_cli=0'; \
    echo 'opcache.memory_consumption=256'; \
    echo 'opcache.interned_strings_buffer=32'; \
    echo 'opcache.max_accelerated_files=20000'; \
    echo 'opcache.save_comments=1'; \
    echo 'opcache.validate_timestamps=0'; \
    echo 'opcache.fast_shutdown=1'; \
    } > /usr/local/etc/php/conf.d/zz-opcache-custom.ini; \
fi

# Install Redis PHP extension.
RUN set -eux; \
    pecl install redis; \
    docker-php-ext-enable redis;

# Install Node.js via NVM for frontend tooling.
ENV NVM_DIR=/root/.nvm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.4/install.sh | bash \
    && . "$NVM_DIR/nvm.sh" \
    && nvm install --lts \
    && nvm alias default 'lts/*' \
    && nvm use default \
    && node -v \
    && npm -v

# Production PHP settings (disable display_errors for clean JSON:API responses).
RUN { \
    echo 'display_errors = Off'; \
    echo 'log_errors = On'; \
    echo 'error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT'; \
    } >> /usr/local/etc/php/conf.d/zz-production.ini

# Note: Drupal modules should be installed via composer in the project directory.
# The /opt/drupal directory is mounted as a volume, so composer require.
# commands here would be lost on container start.
# Install modules by running:
# docker exec drupal-fpm bash -c "cd /opt/drupal && composer require drupal/module_name"
