# syntax=docker/dockerfile:1.6

ARG PHP_VERSION=8.3
ARG NODE_VERSION=20

################################################################################
# Composer dependencies
################################################################################
FROM composer:2 AS vendor

WORKDIR /var/www/html

COPY composer.json composer.lock ./

RUN composer install \
    --no-dev \
    --no-scripts \
    --no-interaction \
    --prefer-dist \
    --optimize-autoloader \
    --ignore-platform-reqs

COPY . .

################################################################################
# Node build stage - compile front-end assets (needs PHP + vendor)
################################################################################
FROM node:${NODE_VERSION}-bookworm AS node_builder

WORKDIR /var/www/html

RUN apt-get update \
 && apt-get install -y --no-install-recommends \
    php-cli \
    php-mbstring \
    php-xml \
    php-curl \
    php-zip \
 && rm -rf /var/lib/apt/lists/*

COPY package.json package-lock.json ./
RUN npm ci --no-audit --prefer-offline

COPY . .
COPY --from=vendor /var/www/html/vendor ./vendor

# Ensure fonts.css exists so Vite can resolve the import even when the
# google fonts sync output is not present in the repository.
RUN mkdir -p public/fonts/google \
 && [ -f public/fonts/google/fonts.css ] || echo '/* placeholder; run php artisan fonts:sync-google to populate */' > public/fonts/google/fonts.css

RUN npm run build

################################################################################
# PHP-FPM runtime image
################################################################################
FROM php:${PHP_VERSION}-fpm-bullseye AS app

ARG UID=1000
ARG GID=1000

ENV APP_ENV=production \
    APP_DEBUG=false \
    PHP_OPCACHE_VALIDATE_TIMESTAMPS=0

WORKDIR /var/www/html

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

COPY . .
COPY --from=vendor /var/www/html/vendor ./vendor
COPY --from=node_builder /var/www/html/public/build ./public/build

RUN php artisan config:clear \
   && php artisan config:cache \
   && php artisan route:clear \
   && php artisan view:clear

RUN mkdir -p /opt/app \
 && rsync -a /var/www/html/ /opt/app/ \
 && chown -R www-data:www-data /opt/app

RUN groupmod -o -g "$GID" www-data \
 && usermod -o -u "$UID" -g www-data www-data

COPY docker/php/php.ini /usr/local/etc/php/conf.d/zz-app.ini
COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/zz-opcache.ini
COPY docker/app/entrypoint.sh /usr/local/bin/app-entrypoint
RUN chmod +x /usr/local/bin/app-entrypoint

EXPOSE 9000

ENTRYPOINT ["app-entrypoint"]
CMD ["php-fpm"]
