# syntax=docker/dockerfile:1

# One image, three roles (selected by the command):
#   web       -> default CMD (FrankenPHP / Caddy on :8080)
#   queue     -> php artisan queue:work
#   one-off   -> php artisan <anything>

ARG FRANKENPHP_VERSION=1.13
ARG PHP_VERSION=8.5

###############################################################################
# base: PHP runtime shared by all stages
###############################################################################
FROM dunglas/frankenphp:${FRANKENPHP_VERSION}-php${PHP_VERSION}-alpine AS base

WORKDIR /app

RUN install-php-extensions \
        intl \
        pcntl \
        pdo_pgsql \
        zip \
    && cp "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# Swiss Ephemeris: the swephp extension (bundles the C library, statically linked)
# plus the data files for planets, Moon and asteroids/Chiron covering 1800-2399.
# Licensing: AGPL or Astrodienst's Professional License - see README.
ARG PHP_SWEPH_VERSION=4.0.11
RUN apk add --no-cache --virtual .sweph-build-deps $PHPIZE_DEPS \
    && mkdir /tmp/php-sweph \
    && curl -fsSL "https://github.com/kevindecapite/php-sweph/archive/refs/tags/${PHP_SWEPH_VERSION}.tar.gz" \
        | tar -xz --strip-components=1 -C /tmp/php-sweph \
    && cd /tmp/php-sweph \
    && phpize \
    && ./configure --with-sweph \
    && make -j"$(nproc)" \
    && make install \
    && docker-php-ext-enable swephp \
    && install -d /usr/share/swisseph \
    && install -m 0644 sweph/ephe/sepl_18.se1 sweph/ephe/semo_18.se1 sweph/ephe/seas_18.se1 /usr/share/swisseph/ \
    && cd / \
    && rm -rf /tmp/php-sweph \
    && apk del .sweph-build-deps

COPY docker/php/app.ini "$PHP_INI_DIR/conf.d/zz-app.ini"

ENV SERVER_NAME=:8080 \
    SERVER_ROOT=/app/public \
    LOG_CHANNEL=stderr

###############################################################################
# assets: the visitor site's CSS, JS and self-hosted fonts (Vite)
###############################################################################
FROM node:22-alpine AS assets

WORKDIR /app

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

COPY vite.config.js ./
COPY resources/css resources/css
COPY resources/js resources/js
RUN npm run build

###############################################################################
# build: composer dependencies + optimized autoloader + Filament assets
###############################################################################
FROM base AS build

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

# Dependencies first so this layer is cached until composer.lock changes.
COPY composer.json composer.lock ./
RUN composer install --no-dev --no-scripts --no-autoloader --prefer-dist --no-interaction --no-progress

COPY . .
COPY --from=assets /app/public/build public/build

RUN mkdir -p storage/app/private storage/app/public storage/framework/cache/data storage/framework/sessions \
        storage/framework/views storage/logs bootstrap/cache \
    && composer dump-autoload --optimize --no-dev --no-interaction \
    && php artisan package:discover --ansi \
    && php artisan filament:assets

###############################################################################
# test: the full suite, including the Swiss Ephemeris tests that need the
# extension:  docker build --target test -t astrologija-test . && docker run --rm astrologija-test
###############################################################################
FROM build AS test

RUN composer install --prefer-dist --no-interaction --no-progress \
    && cp .env.example .env \
    && php artisan key:generate --ansi

CMD ["php", "artisan", "test"]

###############################################################################
# source: the complete source of this build, served at /source.tar.gz so an
# AGPL deployment offers it to every visitor (README, "Swiss Ephemeris
# licence"). The paid interpretation texts (database/passages/<locale>/) are
# content, not program source, and stay out; .dockerignore keeps out secrets.
###############################################################################
FROM base AS source

WORKDIR /src

COPY . .

RUN find database/passages -mindepth 1 -maxdepth 1 -type d -exec rm -rf {} + \
    && tar -czf /source.tar.gz .

###############################################################################
# dist: application files without the test suite
###############################################################################
FROM build AS dist

RUN rm -rf tests phpunit.xml

COPY --from=source /source.tar.gz public/source.tar.gz

###############################################################################
# production: minimal runtime image, runs as non-root
###############################################################################
FROM base AS production

ARG UID=1000
ARG GID=1000

RUN addgroup -g "${GID}" app \
    && adduser -D -u "${UID}" -G app app

# Code stays root-owned (read-only for the app user); only runtime dirs are writable.
COPY --from=dist /app /app
COPY docker/entrypoint.sh /usr/local/bin/app-entrypoint

RUN chmod +x /usr/local/bin/app-entrypoint \
    && chown -R app:app /app/storage /app/bootstrap/cache /data /config

USER app

EXPOSE 8080

ENTRYPOINT ["app-entrypoint"]
CMD ["--config", "/etc/frankenphp/Caddyfile", "--adapter", "caddyfile"]
