# ── Stage 1: install production deps ─────────────────────────────────────
FROM node:22-alpine AS deps
WORKDIR /app/backend
COPY backend/package.json backend/package-lock.json ./
RUN npm ci --omit=dev

# ── Stage 2: runtime ─────────────────────────────────────────────────────
FROM node:22-alpine
LABEL maintainer="BQ-System"

RUN apk add --no-cache sqlite tini

WORKDIR /app
COPY --from=deps /app/backend/node_modules ./backend/node_modules
COPY backend/package.json ./backend/
COPY backend/src           ./backend/src
COPY backend/seed.js       ./backend/seed.js
COPY frontend              ./frontend
COPY js                    ./js

# Ensure data & uploads dirs exist (volumes mount here)
RUN mkdir -p /app/backend/data /app/backend/uploads /app/backups

ENV NODE_ENV=production
ENV PORT=3000
EXPOSE 3000

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD wget -qO- http://localhost:3000/api/health || exit 1

ENTRYPOINT ["tini", "--"]
CMD ["node", "backend/src/server.js"]
