# --- Étape de Build ---
FROM composer:2.7 AS build

WORKDIR /var/www/html

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions intl zip bcmath pdo_pgsql

COPY . .

RUN composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader

# --- Étape Finale (Production) ---
FROM dunglas/frankenphp:1.3-php8.4-alpine

# On installe les extensions pour le fonctionnement de l'app en prod
RUN apk add --no-cache \
    libpq-dev \
    libpng-dev \
    libjpeg-turbo-dev \
    freetype-dev \
    icu-dev

RUN install-php-extensions \
    intl \
    pdo_pgsql \
    gd \
    zip \
    opcache

WORKDIR /var/www/html

COPY --from=build /var/www/html /var/www/html

# Permissions (crucial pour Laravel)
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache

ENV APP_ENV=production
ENV APP_RUNTIME=Laravel\FrankenPHP\Runtime

CMD ["sh", "-c", "php artisan migrate --force && php artisan storage:link && frankenphp run --config /etc/caddy/Caddyfile"]
