# Stage 0: Build Vue frontend assets with Node.js
FROM node:22 AS frontend-builder

WORKDIR /app/frontend

# Copy frontend package files
COPY fe/package*.json ./
COPY fe/pnpm-lock.yaml* ./

# Install pnpm and frontend dependencies (including dev dependencies for build)
RUN npm install -g pnpm
RUN pnpm install --frozen-lockfile

# Copy frontend source code
COPY fe/ ./

# Build TWO frontend bundles (path-based):
# - Production: served at /
# - Training: served at /training/
#
# This avoids runtime JS injection and allows training/prod UI differences
# while keeping a single backend image.
RUN pnpm run typecheck \
    && pnpm exec vite build --mode=production --base=/ --outDir dist-prod \
    && pnpm exec vite build --mode=training --base=/training/ --outDir dist-training

# Clean up dev dependencies to reduce image size
ENV CI=true
RUN pnpm prune --prod

# Stage 1: Build environment and Composer dependencies
FROM php:8.4-fpm AS builder

LABEL maintainer="Topaz"

# Install system dependencies and PHP extensions for Laravel with MySQL/PostgreSQL support.
RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    unzip \
    libpq-dev \
    libonig-dev \
    libssl-dev \
    libxml2-dev \
    libcurl4-openssl-dev \
    libicu-dev \
    libzip-dev \
    libjpeg-dev \
    libpng-dev \
    libfreetype6-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
    pdo_mysql \
    pdo_pgsql \
    pgsql \
    opcache \
    intl \
    zip \
    bcmath \
    soap \
    gd \
    pcntl \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Set the working directory inside the container
WORKDIR /var/www

# Copy the entire Laravel application code into the container
COPY be/ /var/www

# Copy Composer from official image (avoids flaky getcomposer.org in CI)
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer

# Install PHP dependencies
RUN composer install --no-dev --optimize-autoloader --no-interaction --no-progress --prefer-dist --no-scripts \
    && rm -rf bootstrap/cache/services.php bootstrap/cache/packages.php

# Stage 2: Unified production image with PHP-FPM + Nginx
FROM php:8.4-fpm AS base

LABEL maintainer="Topaz"

# Set system timezone to Asia/Kuala_Lumpur
RUN apt-get update && apt-get install -y --no-install-recommends tzdata \
    && ln -snf /usr/share/zoneinfo/Asia/Kuala_Lumpur /etc/localtime \
    && echo "Asia/Kuala_Lumpur" > /etc/timezone \
    && dpkg-reconfigure -f noninteractive tzdata \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install Nginx and all runtime dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    nginx \
    libpq-dev \
    libicu-dev \
    libzip-dev \
    libpng-dev \
    libjpeg-dev \
    libfreetype6-dev \
    libfcgi-bin \
    procps \
    netcat-openbsd \
    supervisor \
    nano \
    curl \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Download and install php-fpm health check script
RUN curl -o /usr/local/bin/php-fpm-healthcheck \
    https://raw.githubusercontent.com/renatomefi/php-fpm-healthcheck/master/php-fpm-healthcheck \
    && chmod +x /usr/local/bin/php-fpm-healthcheck

# Copy the initialization script
COPY be/docker/common/unified/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# Copy supervisor configuration files
COPY be/docker/common/unified/supervisor/supervisord.conf /etc/supervisor/supervisord.conf

# Create supervisor conf.d directory
RUN mkdir -p /etc/supervisor/conf.d

# Use common supervisor and nginx config inside the image.
# If you need environment-specific behavior (training vs production), override at runtime
# by mounting files into:
# - /etc/nginx/nginx.conf
# - /etc/supervisor/conf.d/
COPY be/docker/common/unified/supervisor/unified.conf /etc/supervisor/conf.d/unified.conf
COPY be/docker/common/unified/nginx/nginx.conf /etc/nginx/nginx.conf

# Copy the initial storage structure
COPY be/storage /var/www/storage-init

# Copy PHP extensions and libraries from the builder stage
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/docker-php-ext-* /usr/local/bin/

# Use the recommended production PHP configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# Enable PHP-FPM status page via separate config file (avoid modifying zz-docker.conf)
RUN echo '[www]' > /usr/local/etc/php-fpm.d/zzz-status.conf && \
    echo 'pm.status_path = /status' >> /usr/local/etc/php-fpm.d/zzz-status.conf

# Copy the application code and dependencies from the build stage first
COPY --from=builder /var/www /var/www

# Copy the built Vue frontends from frontend-builder stage
# - Production frontend at /
COPY --from=frontend-builder /app/frontend/dist-prod /var/www/public/
# - Training frontend at /training/
COPY --from=frontend-builder /app/frontend/dist-training /var/www/public/training/

# Set working directory
WORKDIR /var/www

# Ensure correct permissions
RUN chown -R www-data:www-data /var/www

# Create Nginx log directory
RUN mkdir -p /var/log/nginx && chown -R www-data:www-data /var/log/nginx

# Run the entrypoint script
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

# Expose port 80 (Nginx) and 9000 (PHP-FPM for health checks)
EXPOSE 80 9000

# Start supervisor which manages both Nginx and PHP-FPM
CMD ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]
