##############################################################
# GMLM Platform — Production Docker Image
# PHP 8.3-FPM + required extensions
##############################################################

FROM php:8.3-fpm-alpine AS base

LABEL maintainer="GMLM Platform <platform@globalmlmsoftware.com>"
LABEL version="1.0"

# Install system dependencies
RUN apk add --no-cache \
    nginx \
    supervisor \
    curl \
    git \
    unzip \
    libpng-dev \
    libjpeg-turbo-dev \
    libwebp-dev \
    libzip-dev \
    icu-dev \
    oniguruma-dev \
    libxml2-dev \
    redis \
    nodejs \
    npm

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

# Install Redis PHP extension
RUN pecl install redis && docker-php-ext-enable redis

# Install Composer
COPY --from=composer:2.7 /usr/bin/composer /usr/bin/composer

##############################################################
# Development stage
##############################################################
FROM base AS development

WORKDIR /var/www/html

# Copy PHP config for development
COPY docker/php/php-dev.ini /usr/local/etc/php/conf.d/gmlm.ini
COPY docker/php/php-fpm.conf /usr/local/etc/php-fpm.d/www.conf

EXPOSE 9000

##############################################################
# Builder stage — install dependencies and compile assets
##############################################################
FROM base AS builder

WORKDIR /var/www/html

# Copy composer files first (layer caching)
COPY composer.json composer.lock ./
RUN composer install \
    --no-dev \
    --no-interaction \
    --no-scripts \
    --prefer-dist \
    --optimize-autoloader

# Copy package files and install JS dependencies
COPY package.json package-lock.json ./
RUN npm ci --silent

# Copy full application
COPY . .

# Build frontend assets
RUN npm run build

# Run composer post-install scripts now that app is copied
RUN composer dump-autoload --optimize --no-dev

##############################################################
# Production stage
##############################################################
FROM base AS production

WORKDIR /var/www/html

# PHP production configuration
COPY docker/php/php-prod.ini  /usr/local/etc/php/conf.d/gmlm.ini
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

# Nginx configuration
COPY docker/nginx/gmlm.conf   /etc/nginx/nginx.conf

# Supervisor configuration
COPY docker/supervisor/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# Copy built application from builder stage
COPY --from=builder /var/www/html /var/www/html

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

# Healthcheck
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -sf http://localhost/api/v1/health || exit 1

EXPOSE 80 443

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
