# syntax=docker/dockerfile:1
#
# One Dockerfile, three targets:
#
#   docker build -f deploy/Dockerfile --target core   -t dlbot-core .
#   docker build -f deploy/Dockerfile --target worker -t dlbot-worker .
#   docker build -f deploy/Dockerfile --target faketelegram -t dlbot-faketelegram .
#
# docker-compose.yml builds core and worker for you.

ARG GO_VERSION=1.26
ARG NODE_VERSION=24
ARG DEBIAN_VERSION=bookworm

# --- Mini App front end ------------------------------------------------------
# The repository carries a build of the Mini App already. Building it again
# here makes sure the image holds exactly what the sources say.
FROM node:${NODE_VERSION}-${DEBIAN_VERSION}-slim AS miniapp
WORKDIR /src/web/miniapp
COPY web/miniapp/package.json web/miniapp/package-lock.json ./
RUN npm ci --no-audit --no-fund
COPY web/miniapp/ ./
RUN npm run build

# --- build -------------------------------------------------------------------
FROM golang:${GO_VERSION}-${DEBIAN_VERSION} AS build
WORKDIR /src
# go.mod asks for a specific Go patch release. Let the toolchain fetch it
# when the image is a little older.
ENV GOTOOLCHAIN=auto CGO_ENABLED=0
COPY go.mod go.sum ./
RUN go mod download
COPY . .
COPY --from=miniapp /src/web/miniapp/dist ./web/miniapp/dist
RUN go build -trimpath -ldflags "-s -w" -o /out/core ./cmd/core \
 && go build -trimpath -ldflags "-s -w" -o /out/worker ./cmd/worker \
 && go build -trimpath -ldflags "-s -w" -o /out/faketelegram ./cmd/faketelegram

# --- runtime base: ffmpeg and yt-dlp -----------------------------------------
FROM debian:${DEBIAN_VERSION}-slim AS runtime
ARG TARGETARCH
# "latest", or a release tag such as 2026.10.26 to pin the version.
ARG YTDLP_VERSION=latest
RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates curl ffmpeg tini \
 && rm -rf /var/lib/apt/lists/* \
 && useradd --system --uid 10001 --home-dir /data --shell /usr/sbin/nologin dlbot \
 && mkdir -p /data /opt/dlbot/bin \
 && case "${TARGETARCH:-amd64}" in \
      arm64) asset=yt-dlp_linux_aarch64 ;; \
      *)     asset=yt-dlp_linux ;; \
    esac \
 && if [ "$YTDLP_VERSION" = "latest" ]; then \
      url="https://github.com/yt-dlp/yt-dlp/releases/latest/download/$asset"; \
    else \
      url="https://github.com/yt-dlp/yt-dlp/releases/download/$YTDLP_VERSION/$asset"; \
    fi \
 && curl -fsSL -o /opt/dlbot/bin/yt-dlp "$url" \
 && chmod 0755 /opt/dlbot/bin/yt-dlp \
 # yt-dlp updates itself in place, so its directory belongs to the service user.
 && chown -R dlbot:dlbot /data /opt/dlbot
ENV PATH="/opt/dlbot/bin:${PATH}" \
    YTDLP_PATH=/opt/dlbot/bin/yt-dlp \
    FFMPEG_PATH=ffmpeg
USER dlbot
WORKDIR /data
ENTRYPOINT ["/usr/bin/tini", "--"]

# --- core --------------------------------------------------------------------
FROM runtime AS core
COPY --from=build /out/core /usr/local/bin/core
ENV HTTP_LISTEN=:8080 \
    DB_DSN=/data/dlbot.db \
    STORAGE_LOCAL_DIR=/data/files \
    WORKER_TEMP_DIR=/data/tmp
VOLUME ["/data"]
EXPOSE 8080
HEALTHCHECK --interval=15s --timeout=5s --start-period=20s --retries=3 \
  CMD curl -fsS http://127.0.0.1:8080/healthz || exit 1
CMD ["core"]

# --- worker ------------------------------------------------------------------
FROM runtime AS worker
COPY --from=build /out/worker /usr/local/bin/worker
ENV WORKER_TEMP_DIR=/data/tmp \
    WORKER_HEALTH_LISTEN=:8081
VOLUME ["/data"]
HEALTHCHECK --interval=15s --timeout=5s --start-period=20s --retries=3 \
  CMD curl -fsS http://127.0.0.1:8081/healthz || exit 1
CMD ["worker"]

# --- fake Telegram, for trying the bot without a token -----------------------
FROM debian:${DEBIAN_VERSION}-slim AS faketelegram
COPY --from=build /out/faketelegram /usr/local/bin/faketelegram
USER 10001
EXPOSE 8090
CMD ["faketelegram", "-listen", ":8090"]
