# -------- Stage 1: Build stage --------
FROM php:8.3-fpm-alpine AS build

ARG APP_ENV
ARG APP_PORT=80
ENV APP_PORT=${APP_PORT}

# Install build dependencies and required libraries
RUN apk add --no-cache --virtual .build-deps \
        gcc g++ make autoconf \
        freetype-dev libjpeg-turbo-dev libpng-dev \
        oniguruma-dev gettext-dev libzip-dev \
        bash gnupg less unzip curl zip postgresql-dev \
    && apk add --no-cache \
        freetype libjpeg-turbo libpng oniguruma gettext \
        libzip postgresql-libs

# Install PHP extensions (cached unless PHP version changes)
RUN docker-php-ext-configure zip \
    && docker-php-ext-install zip pdo pdo_mysql pdo_pgsql pgsql \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-enable gd \
    && docker-php-ext-install bcmath exif gettext opcache \
    && docker-php-ext-enable bcmath exif gettext opcache

# Install PHP extensions via install-php-extensions
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions intl redis pcntl

# Remove build dependencies early — before copying app code
RUN apk del .build-deps

# Add Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

WORKDIR /var/www/html

# Copy ONLY composer files first — cache bust only when dependencies change
COPY composer.json composer.lock ./

# Install dependencies — strip dev packages only for production
RUN --mount=type=cache,target=/root/.composer/cache \
    if [ "$APP_ENV" = "production" ]; then \
        composer install --optimize-autoloader --no-dev --no-scripts --no-autoloader; \
    else \
        composer install --optimize-autoloader --no-scripts --no-autoloader; \
    fi

# Now copy the full source code
COPY . .

# Run autoloader and scripts after full source is available
RUN if [ "$APP_ENV" = "production" ]; then \
        composer dump-autoload --optimize --no-dev; \
    else \
        composer dump-autoload --optimize; \
    fi \
    && php artisan storage:link

# -------- Stage 2: Runtime stage --------
FROM php:8.3-fpm-alpine

ARG APP_ENV
ENV APP_ENV=${APP_ENV}

# Install runtime dependencies
RUN apk add --no-cache \
    nginx \
    freetype \
    libjpeg-turbo \
    libpng \
    oniguruma \
    gettext \
    bash \
    libstdc++ \
    icu-libs \
    lz4-libs \
    libzip \
    krb5 \
    krb5-libs \
    libc6-compat \
    postgresql-libs \
    supervisor

# Install Supercronic
ENV SUPERCRONIC_VERSION=v0.2.39
ADD https://github.com/aptible/supercronic/releases/download/${SUPERCRONIC_VERSION}/supercronic-linux-amd64 /usr/local/bin/supercronic

WORKDIR /var/www/html

# Copy PHP config and extensions from build
COPY --from=build /usr/local/etc/php /usr/local/etc/php
COPY --from=build /usr/local/lib/php/extensions /usr/local/lib/php/extensions

# Copy Laravel app from build
COPY --from=build /var/www/html /var/www/html

# Copy config files
COPY ./nginx.conf /etc/nginx/http.d/default.conf
RUN mkdir -p /etc/supervisor/conf.d
COPY ./deploy/supervisor/supervisord.conf /etc/supervisord.conf
COPY ./deploy/supervisor/laravel-worker.conf /etc/supervisor/conf.d/laravel-worker.conf
COPY ./deploy/cron/laravel-scheduler /etc/cron.d/laravel-scheduler

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

# Consolidate all permissions in a single layer
RUN chmod +x /usr/local/bin/supercronic \
    && chmod 0644 /etc/cron.d/laravel-scheduler \
    && crontab -u www-data /etc/cron.d/laravel-scheduler \
    && mkdir -p \
        /var/www/html/storage/logs \
        /var/www/html/storage/framework/cache \
        /var/www/html/storage/framework/sessions \
        /var/www/html/storage/framework/views \
    && chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache \
    && chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache \
    && chmod -R 775 /var/www/html/storage/app/public

EXPOSE ${APP_PORT}
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
