# Node.js backend
FROM node:18-alpine

# Build tools needed for better-sqlite3 native compilation
RUN apk add --no-cache python3 make g++

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 with proper ownership
RUN mkdir -p /data && chown node:node /data
VOLUME /data

# Run as non-root user for security
USER node

EXPOSE 3001

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD node -e "require('http').get('http://localhost:3001/api/health',r=>{process.exit(r.statusCode===200?0:1)}).on('error',()=>process.exit(1))"

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