# Multi-stage Dockerfile for Drupal 11 on FlexSite
# Uses FlexSite multi-PHP base images with PHP 8.3 / 8.4 / 8.5 support
#
# PHP version is controlled by .flexsite.yml in your repo:
#   php_version: "8.4"  # or "8.3" / "8.5"
#
# The CI reads this file and passes PHP_VERSION as a build arg.

# Build argument for PHP version (set by CI from .flexsite.yml)
ARG PHP_VERSION=8.4

# Stage 1: Build stage
FROM public.ecr.aws/h2z7x3i5/flexsite-builder:drupal11-arm64 AS builder

ARG PHP_VERSION=8.4
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV COMPOSER_MEMORY_LIMIT=-1

# Switch to the requested PHP version if needed
RUN if [ "$PHP_VERSION" = "8.4" ]; then update-alternatives --set php /usr/bin/php8.4; \
    elif [ "$PHP_VERSION" = "8.5" ]; then update-alternatives --set php /usr/bin/php8.5; fi

WORKDIR /opt/drupal

# Clear the pre-installed Drupal from base image to avoid conflicts
# The builder image may have leftover files that would merge with project source
RUN rm -rf /opt/drupal/* /opt/drupal/.* 2>/dev/null || true && \
    composer clear-cache

# Copy all source code from the repository
COPY . .

# Install PHP dependencies
RUN composer install --no-dev --optimize-autoloader --prefer-dist

# Verify core files exist after install
RUN test -f /opt/drupal/web/core/lib/Drupal.php || (echo "ERROR: Drupal core not properly installed!" && exit 1)

# Install Node.js dependencies and build assets (if package.json exists)
RUN if [ -f "package.json" ]; then \
        npm ci && \
        npm run build 2>/dev/null || true; \
    fi

# Stage 2: Production runtime
FROM public.ecr.aws/h2z7x3i5/flexsite-runtime:drupal11-arm64 AS runtime

ARG PHP_VERSION=8.4

# Set environment variables
# BUILT_WITH_PHP helps the runtime detect version mismatches
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV COMPOSER_MEMORY_LIMIT=-1
ENV BUILT_WITH_PHP=${PHP_VERSION}
ENV PHP_VERSION=${PHP_VERSION}

# CRITICAL: Remove pre-installed Drupal from runtime base image before copying fresh build
# The runtime image may contain /opt/drupal which would otherwise merge with our files
RUN rm -rf /opt/drupal && mkdir -p /opt/drupal

# Copy built application from builder stage (now into empty directory)
COPY --from=builder --chown=www-data:www-data /opt/drupal /opt/drupal

# Copy configuration files
# NOTE: nginx.conf is in the base image, drupal.conf is mounted from ConfigMap
# This allows GitOps to control nginx server config including basic auth
COPY docker/nginx.conf /etc/nginx/nginx.conf
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY docker/syslog-forward.py /etc/supervisor/conf.d/syslog-forward.py

# Create symlink for web root (matches base image pattern)
# nginx expects /var/www/html/web, so create /var/www/html as directory with web symlink inside
RUN rm -rf /var/www/html && mkdir -p /var/www/html && ln -sf /opt/drupal/web /var/www/html/web

# Set proper permissions
RUN chown -R www-data:www-data /opt/drupal && \
    chmod -R 755 /opt/drupal && \
    chmod 775 /opt/drupal/web/sites/default && \
    mkdir -p /opt/drupal/web/sites/default/files/{css,js,tmp} && \
    mkdir -p /opt/drupal/web/modules/custom && \
    mkdir -p /opt/drupal/web/themes/custom && \
    mkdir -p /opt/drupal/web/themes/flexsite && \
    chown -R www-data:www-data /opt/drupal/web/sites/default/files && \
    chmod -R 775 /opt/drupal/web/sites/default/files

# Create health check file
RUN echo '<?php echo "OK"; ?>' > /opt/drupal/web/health.php && \
    chown www-data:www-data /opt/drupal/web/health.php

# Expose port
EXPOSE 8080

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8080/health.php || exit 1

# Start via entrypoint (from base image)
CMD ["/docker-entrypoint.sh"]
