FROM php:8.4-fpm-alpine AS base

ARG HOST_UID=1000
ARG HOST_GID=1000

RUN apk add --no-cache \
        libzip-dev \
        zlib-dev \
        unzip \
        git \
        curl \
        shadow \
    && docker-php-ext-install zip pcntl \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && groupmod -g $HOST_GID www-data \
    && usermod -u $HOST_UID -g $HOST_GID www-data

WORKDIR /var/www/html

COPY docker/php/entrypoint.sh /usr/local/bin/entrypoint.sh

# --- builder: composer install without dev deps, no scripts ---
FROM base AS builder
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --no-plugins --prefer-dist --no-interaction

# --- runtime: copy app code on top of vendor/ ---
FROM base AS runtime
COPY --from=builder /var/www/html/vendor /var/www/html/vendor
COPY composer.json composer.lock ./
COPY bin ./bin
COPY config ./config
COPY fixtures ./fixtures
COPY public ./public
COPY src ./src
COPY storage ./storage

RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["php-fpm"]
