# Node.js backend
FROM node:18-alpine

# Build tools needed for better-sqlite3 native compilation
# su-exec for dropping privileges in entrypoint
RUN apk add --no-cache python3 make g++ su-exec

WORKDIR /app

COPY package*.json ./

RUN npm install --omit=dev

# Remove build tools after install to keep image small
RUN apk del python3 make g++

COPY . .

# Create data directory for SQLite
RUN mkdir -p /data
VOLUME /data

# Entrypoint fixes /data permissions then drops to node user
COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

EXPOSE 3001

HEALTHCHECK --interval=10s --timeout=5s --start-period=15s --retries=3 \
  CMD wget --spider -q http://localhost:3001/api/health || exit 1

ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["node", "server.js"]
