FROM node:20-alpine

# python + build tools needed for better-sqlite3 native module
RUN apk add --no-cache python3 make g++

WORKDIR /app

# Install deps first for cached layer
COPY package.json ./
RUN npm install --omit=dev

# App code
COPY src ./src
COPY public ./public

# Persistent data dir
RUN mkdir -p /data/media && chown -R node:node /data
VOLUME ["/data"]

USER node

ENV NODE_ENV=production
ENV PORT=3000
ENV DATA_DIR=/data

EXPOSE 3000

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

CMD ["node", "src/index.js"]
