FROM node:20-alpine AS base

# Build
FROM base AS builder
WORKDIR /app
COPY package.json package-lock.json* ./

# Copy pre-built @bytelyst/* packages (run scripts/docker-prep-dashboards.sh first)
# file: refs point to ../../learning_ai_common_plat/packages/* relative to /app
COPY .docker-deps/@bytelyst/ /learning_ai_common_plat/packages/

RUN npm ci
COPY . .
# Dummy env vars for Next.js build (page data collection requires these at build time)
ENV COSMOS_ENDPOINT=https://placeholder.documents.azure.com:443/
ENV COSMOS_KEY=placeholder==
ENV COSMOS_DATABASE=lysnrai
ENV JWT_SECRET=build-time-placeholder
RUN npm run build

# Production
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3001
ENV PORT=3001
ENV HOSTNAME="0.0.0.0"

CMD ["node", "server.js"]
