# iya.ru platform — runtime image (P16)
#
# Build:  docker build -t iya-platform .
# Run:    docker run -d -p 8080:8080 -v iya-data:/app/var iya-platform
#
# Design notes:
#  * php:8.2 CLI on Alpine — the platform is stdlib-only (no composer at
#    runtime; composer deps exist only for tests/, which CI runs separately).
#  * SQLite lives under /app/var (VOLUME) so the DB, uploads, cache and logs
#    survive container recreation.
#  * migrate-on-boot: the entrypoint applies pending migrations before
#    serving, matching the zero-ops deployment story.
#  * Worker: run a second container with `docker run ... iya-platform
#    php bin/iya worker` on a 1-minute schedule/cron, or a long-lived
#    `while true; do php bin/iya worker --once; sleep 60; done` loop.

FROM php:8.2-cli-alpine

# Runtime libs + build deps for the required extensions (ADR-001 contract:
# sqlite3, pdo_sqlite, mbstring, intl, zip, curl; opcache for production).
RUN set -eux; \
    apk add --no-cache --virtual .build-deps \
        icu-dev libzip-dev oniguruma-dev curl-dev sqlite-dev; \
    apk add --no-cache \
        icu-libs libzip oniguruma curl sqlite-libs sqlite; \
    docker-php-ext-install -j"$(nproc)" \
        pdo_sqlite sqlite3 mbstring intl zip curl opcache; \
    apk del .build-deps

WORKDIR /app

# App code (var/, .git and archives excluded via .dockerignore).
COPY . /app

# Sensible defaults; override with -e / --env-file in real deployments.
ENV APP_ENV=production \
    DB_DRIVER=sqlite \
    DB_SQLITE_PATH=/app/var/data/iya.sqlite3 \
    IP_BLOCKS_ENFORCE=true \
    RATE_LIMIT_DEFAULT_ENFORCE=true

VOLUME ["/app/var"]
EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
    CMD wget -qO /dev/null --header="Host: iya.ru" http://127.0.0.1:8080/api/v1/health || exit 1

CMD ["sh", "-c", "php bin/iya migrate && exec php bin/iya serve --host=0.0.0.0 --port=8080"]
