FROM node:18-alpine

WORKDIR /app

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

# Ensure shared pipeline schema can be applied automatically when missing
ENV APPLY_SCHEMAS_SQL=true

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy source code
COPY . .

# Create non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S template-manager -u 1001

# Change ownership
RUN chown -R template-manager:nodejs /app
USER template-manager

# Expose port
EXPOSE 8009

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:8009/health || exit 1

# Start the application
CMD ["npm", "start"]