# CluPilot — app runtime image (dev + prod base).
# One image runs php-fpm + nginx + vite (via supervisor) for the `app` service,
# and is reused with an overridden command for `reverb` and `queue`.
FROM php:8.3-fpm-bookworm

ARG HOST_UID=1000
ARG HOST_GID=1000

# --- system packages -------------------------------------------------------
RUN apt-get update && apt-get install -y --no-install-recommends \
        nginx supervisor git unzip curl ca-certificates gnupg \
        libpng-dev libjpeg62-turbo-dev libfreetype6-dev \
        libzip-dev libonig-dev libicu-dev \
        wireguard-tools iproute2 openssh-client \
    && rm -rf /var/lib/apt/lists/*

# --- php extensions --------------------------------------------------------
RUN docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j"$(nproc)" \
        pdo_mysql mbstring exif pcntl bcmath gd zip intl \
    && pecl install redis \
    && docker-php-ext-enable redis

# --- node 22 (for vite) ----------------------------------------------------
RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
    && apt-get install -y --no-install-recommends nodejs \
    && rm -rf /var/lib/apt/lists/*

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

# --- match host uid/gid so bind-mounted files stay writable ---------------
RUN groupmod -o -g "${HOST_GID}" www-data \
    && usermod  -o -u "${HOST_UID}" -g "${HOST_GID}" www-data

# --- config ----------------------------------------------------------------
COPY docker/php/php.ini            /usr/local/etc/php/conf.d/zz-clupilot.ini
COPY docker/nginx/default.conf     /etc/nginx/sites-enabled/default
COPY docker/supervisor/clupilot.conf /etc/supervisor/conf.d/clupilot.conf
COPY docker/entrypoint.sh          /usr/local/bin/clupilot-entrypoint
RUN chmod +x /usr/local/bin/clupilot-entrypoint

WORKDIR /var/www/html
EXPOSE 80 5173 8080

# Entrypoint bootstraps vendor/node_modules on a fresh checkout (see script).
ENTRYPOINT ["clupilot-entrypoint"]
CMD ["supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"]
