# FlickNexs Enhanced Analytics API - Enterprise Dockerfile
# Multi-stage build for optimized production image

# ===================================
# Stage 1: Base Dependencies
# ===================================
FROM node:18-alpine AS dependencies

LABEL maintainer="FlickNexs Development Team"
LABEL version="2.0.0"
LABEL description="FlickNexs Enhanced Analytics API - Enterprise Edition"

# Set working directory
WORKDIR /app

# Install system dependencies for native modules
RUN apk add --no-cache \
    python3 \
    make \
    g++ \
    curl \
    bash \
    dumb-init \
    && npm install -g pm2

# Copy package files
COPY package*.json ./

# Install all dependencies (including dev dependencies for building)
RUN npm ci --only=production --silent && \
    npm cache clean --force

# ===================================
# Stage 2: Development Environment
# ===================================
FROM dependencies AS development

# Install dev dependencies
RUN npm ci --silent

# Copy application source code
COPY . .

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

# Set ownership
RUN chown -R analytics:nodejs /app
USER analytics

# Expose port
EXPOSE 3001

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

# Development command
CMD ["npm", "run", "dev"]

# ===================================
# Stage 3: Production Builder
# ===================================
FROM dependencies AS builder

# Copy application source
COPY . .

# Run any build steps (if needed)
RUN npm run lint:fix || true

# Create production-only node_modules
RUN npm prune --production

# ===================================
# Stage 4: Production Runtime
# ===================================
FROM node:18-alpine AS production

# Install runtime dependencies
RUN apk add --no-cache \
    curl \
    bash \
    dumb-init \
    tini

# Set working directory
WORKDIR /app

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

# Copy production dependencies
COPY --from=builder /app/node_modules ./node_modules

# Copy application code
COPY --from=builder --chown=analytics:nodejs /app .

# Remove unnecessary files for production
RUN rm -rf \
    coverage/ \
    tests/ \
    *.test.js \
    *.spec.js \
    .git/ \
    .gitignore \
    Dockerfile* \
    docker-compose* \
    README*.md

# Set proper permissions
RUN chown -R analytics:nodejs /app && \
    chmod -R 755 /app

# Switch to non-root user
USER analytics

# Create logs directory
RUN mkdir -p /app/logs

# Set environment variables
ENV NODE_ENV=production
ENV PORT=3001
ENV LOG_LEVEL=info

# Expose port
EXPOSE 3001

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

# Use tini as init system
ENTRYPOINT ["/sbin/tini", "--"]

# Start application with PM2 for production
CMD ["dumb-init", "node", "server.js"]

# ===================================
# Stage 5: Production with PM2
# ===================================
FROM production AS production-pm2

# Switch back to root to install PM2
USER root

# Install PM2 globally
RUN npm install -g pm2@latest

# Copy PM2 configuration
COPY --chown=analytics:nodejs ecosystem.config.js ./

# Switch back to non-root user
USER analytics

# Start with PM2
CMD ["pm2-runtime", "start", "ecosystem.config.js"]

# ===================================
# Labels for production image
# ===================================
LABEL org.opencontainers.image.title="FlickNexs Enhanced Analytics API"
LABEL org.opencontainers.image.description="Enterprise-grade multi-tenant analytics platform"
LABEL org.opencontainers.image.version="2.0.0"
LABEL org.opencontainers.image.vendor="FlickNexs"
LABEL org.opencontainers.image.licenses="PROPRIETARY"
LABEL org.opencontainers.image.source="https://github.com/flicknexs/analytics-enterprise"
LABEL org.opencontainers.image.documentation="https://github.com/flicknexs/analytics-enterprise/wiki"
