FROM node:24-slim AS web-builder
WORKDIR /web
COPY ./apps/web/ .
RUN npm ci && npm run build

# FROM golang:1.24-alpine3.22 AS builder
FROM golang:1.24-bookworm AS builder
WORKDIR /build
COPY apps/api/ ./api
COPY apps/book-reaper/ ./book-reaper
COPY --from=web-builder /web/dist ./api/web

ENV GOCACHE=/root/.cache/go-build
ENV CGO_ENABLED=1
ENV GOPROXY=goproxy.io
RUN --mount=type=cache,target="/root/.cache/go-build" cd ./api && go build --tags=fts5 -ldflags="-s -w" -o ../yabl .
RUN --mount=type=cache,target="/root/.cache/go-build" cd ./book-reaper && go build --tags=fts5 -ldflags="-s -w" -o ../book-reaper ./cmd/book-reaper

# FROM alpine:3.22
FROM debian:bookworm-slim

WORKDIR /app

COPY --from=builder /build/yabl .
COPY --from=builder /build/book-reaper .

RUN chmod +x /app/yabl /app/book-reaper
# RUN apk add mupdf-libs libffi-dev

ENTRYPOINT ["/app/yabl"]