# ─────────────────────────────────────────────────────────────
# WASL Digital Wallet — Production Dockerfile
# Multi-stage build: smaller final image, no dev tools in prod.
# Base: PHP 8.3 with Swoole, PDO_PGSQL, Redis, BCMath, OpenSS
# ─────────────────────────────────────────────────────────────

# ---- Base stage: system + PHP extensions ----
FROM php:8.3-fpm-alpine AS base

# System dependencies
RUN apk add --no-cache \
    nginx \
    bash \
    curl \
    git \
    zip \
    unzip \
    libzip-dev \
    libpng-dev \
    libjpeg-turbo-dev \
    freetype-dev \
    libxml2-dev \
    oniguruma-dev \
    postgresql-dev \
    linux-headers \
    $PHPIZE_DEPS

# PHP extensions required by WASL
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) \
        pdo \
        pdo_pgsql \
        pgsql \
        gd \
        zip \
        bcmath \
        opcache \
        pcntl \
        intl \
        exif

# Redis via PECL
RUN pecl install redis && docker-php-ext-enable redis

# Swoole via PECL (required for Octane)
RUN pecl install swoole && docker-php-ext-enable swoole

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

WORKDIR /var/www/html

# ─────────────────────────────────────────────────────────────
# Dependencies stage — caches vendor/ separately from source
# ─────────────────────────────────────────────────────────────
FROM base AS deps

COPY composer.json composer.lock ./
# If no lock file yet, composer will resolve; otherwise install exact versions
RUN composer install --no-interaction --no-scripts --no-autoloader --prefer-dist

# ─────────────────────────────────────────────────────────────
# Development stage — full tooling, debug helpers
# ─────────────────────────────────────────────────────────────
FROM deps AS development

RUN composer install --no-interaction --prefer-dist

COPY . .
RUN composer dump-autoload --optimize

# Xdebug and development PHP config
RUN pecl install xdebug && docker-php-ext-enable xdebug
COPY docker/php/dev.ini /usr/local/etc/php/conf.d/99-wasl-dev.ini

# Supervisor for Octane + queue workers
RUN apk add --no-cache supervisor
COPY docker/supervisor/supervisord.conf /etc/supervisord.conf

EXPOSE 8000
CMD ["php", "artisan", "octane:start", "--server=swoole", "--host=0.0.0.0", "--port=8000"]

# ─────────────────────────────────────────────────────────────
# Production stage — optimized, no dev dependencies, opcache
# ─────────────────────────────────────────────────────────────
FROM base AS production

# Production php.ini
COPY docker/php/production.ini /usr/local/etc/php/conf.d/99-wasl.ini

# Copy application source
COPY --chown=www-data:www-data . /var/www/html

# Install ONLY production dependencies (no dev)
COPY composer.json composer.lock ./
RUN composer install --no-interaction --no-dev --optimize-autoloader --no-scripts \
    && composer dump-autoload --no-dev --optimize

# Ensure correct permissions for Laravel storage
RUN chown -R www-data:www-data /var/www/html \
    && chmod -R 775 storage bootstrap/cache

# Supervisor to run Octane + queue workers within one container
RUN apk add --no-cache supervisor
COPY docker/supervisor/supervisord.conf /etc/supervisord.conf

EXPOSE 8000

# Run supervisor (manages Octane + queue workers)
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
