# =============================================================================
# STAGE 1: Build frontend assets (Node.js)
# =============================================================================
FROM node:20-alpine AS node-builder

WORKDIR /app

# Copy package files first (layer cache optimization)
COPY package.json package-lock.json ./

# Install node dependencies
RUN npm ci --prefer-offline

# Copy source files needed for Vite build
COPY resources/ ./resources/
COPY vite.config.js ./
COPY public/ ./public/

# Build production assets
RUN npm run build


# =============================================================================
# STAGE 2: Install PHP dependencies (Composer)
# =============================================================================
FROM composer:2.7 AS composer-builder

WORKDIR /app

# Copy composer files first (layer cache)
COPY composer.json composer.lock ./

# Install dependencies (no dev, no scripts, no autoloader yet)
RUN composer install \
    --no-dev \
    --no-scripts \
    --no-autoloader \
    --prefer-dist \
    --ignore-platform-reqs

# Copy application source
COPY . .

# Generate optimized autoloader
RUN composer dump-autoload --optimize --no-dev


# =============================================================================
# STAGE 3: Production image (PHP-FPM + Nginx + OPcache + Redis)
# =============================================================================
FROM php:8.2-fpm-alpine AS production

LABEL maintainer="ujian-online"
LABEL description="Laravel 12 Ujian Online - Production Image"

# --- Install system dependencies ---
RUN apk add --no-cache \
    nginx \
    supervisor \
    curl \
    libpng-dev \
    libjpeg-turbo-dev \
    libwebp-dev \
    freetype-dev \
    libzip-dev \
    icu-dev \
    oniguruma-dev \
    libxml2-dev \
    linux-headers \
    $PHPIZE_DEPS

# --- Install PHP extensions ---
RUN docker-php-ext-configure gd \
        --with-freetype \
        --with-jpeg \
        --with-webp \
    && docker-php-ext-install -j$(nproc) \
        pdo_mysql \
        mbstring \
        exif \
        pcntl \
        bcmath \
        gd \
        zip \
        intl \
        opcache

# --- Install Redis extension via PECL ---
RUN pecl install redis \
    && docker-php-ext-enable redis \
    && rm -rf /tmp/pear

# --- Clean up build tools ---
RUN apk del $PHPIZE_DEPS \
    && rm -rf /var/cache/apk/*

# --- Copy custom PHP & FPM configs ---
COPY docker/php/php-fpm.conf /usr/local/etc/php-fpm.d/www.conf
COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
COPY docker/php/php.ini /usr/local/etc/php/conf.d/custom.ini

# --- Copy Nginx config ---
COPY docker/nginx/nginx.conf /etc/nginx/nginx.conf

# --- Copy Supervisor config ---
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# --- Copy entrypoint script ---
COPY docker/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# --- Set working directory ---
WORKDIR /var/www/html

# --- Copy PHP vendor dari composer stage ---
COPY --from=composer-builder /app/vendor ./vendor

# --- Copy built frontend assets dari node stage ---
COPY --from=node-builder /app/public/build ./public/build

# --- Copy seluruh aplikasi ---
COPY --from=composer-builder /app .

# --- Set permissions ---
RUN chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html/storage \
    && chmod -R 755 /var/www/html/bootstrap/cache

# --- Create Nginx log & run dirs ---
RUN mkdir -p /var/log/nginx /run/nginx /var/log/supervisor

EXPOSE 80

# --- Entrypoint: cache Laravel configs then run Supervisor ---
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
