# syntax=docker/dockerfile:1
# V11 Enterprise QMS — single-container image (Next.js + Prisma + SQLite)

# ─── Builder ──────────────────────────────────────────────────────────────────
FROM node:20-alpine AS builder
WORKDIR /app

# Install deps (legacy-peer-deps comes from .npmrc — needed for React 19)
COPY package.json package-lock.json .npmrc ./
RUN npm ci

# Build (prisma generate && next build). DATABASE_URL is only needed at runtime;
# a dummy value satisfies prisma generate during the image build.
COPY . .
ENV DATABASE_URL="file:/data/qms.db"
RUN npm run build

# ─── Runner ───────────────────────────────────────────────────────────────────
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV DATABASE_URL="file:/data/qms.db"

# Production deps only (keeps next + prisma CLI + @prisma/client; drops build tooling)
COPY package.json package-lock.json .npmrc ./
RUN npm ci --omit=dev

# Prisma schema + generated client for the prod node_modules
COPY prisma ./prisma
RUN npx prisma generate

# App build output and runtime files
COPY --from=builder /app/.next ./.next
COPY next.config.js ./
COPY scripts ./scripts
COPY docker-entrypoint.sh ./

# /data holds the SQLite file and is mounted from the Unraid host
RUN mkdir -p /data && chmod +x docker-entrypoint.sh

EXPOSE 3000
ENTRYPOINT ["./docker-entrypoint.sh"]
