# Stage 1: Frontend bauen
FROM node:22-slim AS frontend-builder
WORKDIR /app/frontend
RUN corepack enable
COPY frontend/package.json frontend/pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile
COPY frontend/ ./
RUN pnpm build

# Stage 2: Go-Binary bauen (CGO nötig für go-sqlite3)
FROM golang:1.24-bookworm AS go-builder
WORKDIR /app
COPY backend/go.mod backend/go.sum ./
RUN go mod download
COPY backend/ ./
# Frontend-Build in static/ einhängen (wird per embed eingebettet)
COPY --from=frontend-builder /app/frontend/dist/ ./static/
RUN CGO_ENABLED=1 go build -ldflags="-s -w" -o krafttrainer ./cmd/server

# Stage 3: Minimales Runtime-Image
FROM debian:bookworm-slim
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# DB landet im Volume /data (hardcodierter Pfad "krafttrainer.db" → WORKDIR)
WORKDIR /data
COPY --from=go-builder /app/krafttrainer /usr/local/bin/krafttrainer

EXPOSE 8090
CMD ["krafttrainer"]
