# syntax=docker/dockerfile:1
# Clusev — multi-stage image.
#   dev   : php-fpm + nginx + supervisor + composer + node (source bind-mounted)
#   prod  : baked source + vendor + built assets, no node
# See CLAUDE.md §7/§8. Host has only Docker — everything runs in here.

ARG PHP_VERSION=8.3

# ============================================================================
# base — php-fpm + nginx + supervisor + PHP extensions
# ============================================================================
FROM php:${PHP_VERSION}-fpm-bookworm AS base

ARG APP_UID=1002
ARG APP_GID=1002

RUN apt-get update && apt-get install -y --no-install-recommends \
        nginx supervisor git unzip \
        libzip-dev libpng-dev libjpeg62-turbo-dev libfreetype6-dev \
        libicu-dev libonig-dev \
        default-mysql-client \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" \
        pdo_mysql mbstring zip gd intl bcmath pcntl opcache exif \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && rm -rf /var/lib/apt/lists/*

# Composer (from the official image)
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Non-root user matching the host UID/GID → clean bind-mount writes in dev
RUN groupadd -g ${APP_GID} app \
    && useradd -u ${APP_UID} -g app -m -s /bin/sh app

# Runtime configs
COPY docker/php/php.ini                 /usr/local/etc/php/conf.d/zz-clusev.ini
COPY docker/php/www.conf                /usr/local/etc/php-fpm.d/www.conf
COPY docker/nginx/nginx.conf            /etc/nginx/nginx.conf
COPY docker/supervisor/supervisord.conf /etc/supervisor/conf.d/clusev.conf
COPY docker/entrypoint.sh               /usr/local/bin/entrypoint
RUN chmod +x /usr/local/bin/entrypoint

WORKDIR /var/www/html
EXPOSE 80
ENTRYPOINT ["entrypoint"]
CMD ["supervisord", "-c", "/etc/supervisor/conf.d/clusev.conf"]

# ============================================================================
# dev — add Node 20 for Vite; source is bind-mounted at runtime (no COPY)
# ============================================================================
FROM base AS dev
COPY --from=node:20-bookworm-slim /usr/local/bin/node             /usr/local/bin/node
COPY --from=node:20-bookworm-slim /usr/local/lib/node_modules     /usr/local/lib/node_modules
RUN ln -sf /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
    && ln -sf /usr/local/lib/node_modules/npm/bin/npx-cli.js /usr/local/bin/npx

# Dev runs Vite (HMR) too — php-fpm + nginx + vite via supervisor in this one container.
COPY docker/supervisor/supervisord.dev.conf /etc/supervisor/conf.d/clusev.conf

# ============================================================================
# vendor — prod composer dependencies (no dev deps)
# ============================================================================
FROM base AS vendor
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist
COPY . .
RUN composer dump-autoload --optimize --no-dev

# ============================================================================
# assets — prod Vite build
# ============================================================================
FROM node:20-bookworm-slim AS assets
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci || npm install
COPY . .
RUN npm run build

# ============================================================================
# prod — baked source + vendor + assets
# ============================================================================
FROM base AS prod
COPY . .
COPY --from=vendor /var/www/html/vendor ./vendor
COPY --from=assets /app/public/build ./public/build
RUN rm -f .env && chown -R app:app storage bootstrap/cache
