FROM node:18-alpine

# Install wget for health checks
RUN apk add --no-cache wget

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production && npm cache clean --force

# Copy application files
COPY server.js ./
COPY public ./public
COPY routes ./routes
COPY services ./services
COPY config ./config
COPY controllers ./controllers
COPY middleware ./middleware
COPY utils ./utils
COPY templates ./templates

# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nodejs -u 1001

# Change ownership
RUN chown -R nodejs:nodejs /app

USER nodejs

EXPOSE 3000

# Use exec form to ensure PID 1 is node process
CMD ["node", "server.js"]