# syntax=docker/dockerfile:1

# Stage 1: PHP dependencies (Flux Pro auth injected as a BuildKit secret)
FROM docker.io/library/composer:2 AS composer-builder
WORKDIR /app
COPY composer.json composer.lock ./
RUN --mount=type=secret,id=composer_auth,target=/tmp/auth.json \
    composer install \
    --no-dev \
    --no-interaction \
    --no-plugins \
    --no-scripts \
    --prefer-dist \
    --optimize-autoloader
COPY . .
RUN composer dump-autoload --optimize --no-dev

# Stage 2: frontend assets — needs vendor/livewire/flux CSS present
FROM docker.io/library/node:20-alpine AS node-builder
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
COPY --from=composer-builder /app/vendor ./vendor
RUN npm run build

# Stage 3: production image — FrankenPHP serves the app (no nginx, no supervisor)
FROM docker.io/dunglas/frankenphp:1.12-php8.5

RUN install-php-extensions pdo_pgsql pgsql intl zip opcache

WORKDIR /app

COPY --from=composer-builder /app /app
COPY --from=node-builder /app/public/build /app/public/build

COPY docker/Caddyfile /etc/caddy/Caddyfile
COPY docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh \
    && mkdir -p storage/framework/{sessions,views,cache/data} storage/logs bootstrap/cache \
    && chown -R www-data:www-data /app/storage /app/bootstrap/cache

EXPOSE 8080

ENTRYPOINT ["/entrypoint.sh"]