# syntax=docker/dockerfile:1.6

#################################
# Stage 1: Build PHP + Composer #
#################################
FROM php:8.4-fpm-alpine AS php_build

# System deps for PHP extensions
RUN set -eux; \
    apk add --no-cache git unzip icu-dev libzip-dev oniguruma-dev postgresql-dev autoconf build-base

# PHP extensions needed by Laravel
RUN set -eux; \
    docker-php-ext-configure zip; \
    docker-php-ext-install -j"$(nproc)" \
        pdo_mysql pdo_pgsql pgsql zip intl mbstring bcmath opcache

# Composer
ENV COMPOSER_ALLOW_SUPERUSER=1 COMPOSER_HOME=/tmp/composer
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer

WORKDIR /app

# --- PASS 1: deps only (cacheable), NO scripts (artisan not present yet) ---
COPY composer.json composer.lock ./
RUN composer install \
    --no-dev --no-interaction --prefer-dist \
    --no-scripts --no-progress --optimize-autoloader

# Now copy the whole application
COPY . .

# --- PASS 2: run scripts now that artisan exists ---
# (This is quick: vendor is already populated; this mainly runs package:discover)
RUN composer install \
    --no-dev --no-interaction --prefer-dist \
    --optimize-autoloader

#################################
# Stage 2: Build Frontend (Vite)#
#################################
FROM node:22-alpine AS node_build
WORKDIR /app

# Bring full app INCLUDING vendor so imports like ../../vendor/... work
COPY --from=php_build /app /app

RUN apk add --no-cache python3 make g++

# Install JS deps with the appropriate lockfile
RUN set -eux; \
    if [ -f pnpm-lock.yaml ]; then corepack enable && pnpm i --frozen-lockfile; \
    elif [ -f yarn.lock ]; then corepack enable && yarn install --frozen-lockfile; \
    elif [ -f package-lock.json ]; then npm ci; \
    else npm i; fi

ENV NODE_ENV=production
RUN npm run build

#################################
# Stage 3: Runtime Image        #
#################################
FROM php:8.4-fpm-alpine

# Runtime packages
RUN set -eux; \
    apk add --no-cache nginx supervisor curl icu-libs libzip oniguruma libpq

# PHP runtime extensions
RUN set -eux; \
    apk add --no-cache icu-dev libzip-dev oniguruma-dev postgresql-dev autoconf build-base; \
    docker-php-ext-configure zip; \
    docker-php-ext-install -j"$(nproc)" \
        pdo_mysql pdo_pgsql pgsql zip intl mbstring bcmath opcache; \
    apk del --no-progress --purge icu-dev libzip-dev oniguruma-dev postgresql-dev autoconf build-base || true

# php.ini production + a bit of opcache tuning
RUN set -eux; \
    mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"; \
    { \
      echo "opcache.enable=1"; \
      echo "opcache.enable_cli=0"; \
      echo "opcache.memory_consumption=192"; \
      echo "opcache.max_accelerated_files=40000"; \
      echo "opcache.validate_timestamps=0"; \
      echo "realpath_cache_size=4096K"; \
      echo "realpath_cache_ttl=600"; \
    } >> "$PHP_INI_DIR/conf.d/99-opcache.ini"

# FPM config (listen on TCP for nginx)
RUN set -eux; \
    sed -ri 's|^;?clear_env\s*=.*|clear_env = no|g' /usr/local/etc/php-fpm.d/www.conf; \
    sed -ri 's|^listen = .*|listen = 127.0.0.1:9000|g' /usr/local/etc/php-fpm.d/www.conf; \
    sed -ri 's|^user\s*=.*|user = nginx|g' /usr/local/etc/php-fpm.d/www.conf; \
    sed -ri 's|^group\s*=.*|group = nginx|g' /usr/local/etc/php-fpm.d/www.conf; \
    { echo "ping.path = /ping"; echo "pm.status_path = /status"; echo "catch_workers_output = yes"; } >> /usr/local/etc/php-fpm.d/www.conf

# Paths
RUN set -eux; \
    mkdir -p /run/nginx /var/log/nginx /var/log/supervisor \
             /etc/nginx/conf.d /var/www/html \
             /var/cache/nginx /var/lib/nginx/tmp

WORKDIR /var/www/html

# App + built assets
COPY --from=php_build  --chown=nginx:nginx /app /var/www/html
COPY --from=node_build --chown=nginx:nginx /app/public/build /var/www/html/public/build

# Configs (use the nginx site config you posted)
COPY ./docker/nginx.conf       /etc/nginx/nginx.conf
COPY ./docker/site.conf        /etc/nginx/conf.d/default.conf
COPY ./docker/supervisord.conf /etc/supervisord.conf
COPY ./docker/entrypoint.sh    /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# Logs to STDOUT/ERR
RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log

# Laravel writable dirs & perms
RUN mkdir -p storage/framework/{cache,sessions,views} storage/logs bootstrap/cache && \
    chown -R nginx:nginx storage bootstrap/cache && \
    chmod -R ug+rwX storage bootstrap/cache

ENV APP_ENV=production APP_DEBUG=false APP_URL=http://localhost APP_PORT=8080
EXPOSE 8080

USER nginx:nginx
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]
