FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev

FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production

RUN addgroup --system --gid 1001 nodejs && \
    adduser --system --uid 1001 express

COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN mkdir -p public/images && chown -R express:nodejs /app

COPY docker/seed/ /app/seed/
COPY docker/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

USER express
EXPOSE 4000

HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:4000/auth/me || exit 1

ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["node", "index.js"]
