# aiw-voice-backend: Fastify API + admin panel + KB conversion worker.
# Multi-stage so node_modules dev deps don't ship to the runtime image.

FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Build outputs:
#   /app/dist/server.js
#   /app/dist/lib/db/migrate.js   ← used by the `migrate` compose service

FROM node:20-alpine
WORKDIR /app

# Install only runtime deps.
COPY package*.json ./
RUN npm ci --omit=dev

# Compiled JS + Drizzle migration SQL.
# Note: /app/data (golden-answers.json, kb/) is gitignored and only used by
# the analytics / guardrails-test / kb-text-fallback modules which are
# excluded from the current build per tsconfig.json. Do NOT re-add a COPY
# for it here without first un-ignoring those files in the repo's .gitignore.
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/drizzle ./drizzle

# Static admin panel - served at /admin/ by Fastify (see src/app.ts).
# app.ts uses path.join(__dirname, '..', 'admin') → /app/admin at runtime.
COPY --from=builder /app/admin ./admin

# Storage volume mount-point. The compose file binds a host directory here so
# uploaded KB files survive container restarts.
ENV STORAGE_BASE_PATH=/app/storage
RUN mkdir -p /app/storage

EXPOSE 3000
CMD ["node", "dist/server.js"]
