# Multi-stage build for AI Developer Assistant
FROM node:20-alpine AS builder

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./
COPY tsconfig.json ./

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

# Copy source code
COPY src/ ./src/

# Build the application
RUN npm run build

# Production stage
FROM node:20-alpine AS production

# Install git for Git operations
RUN apk add --no-cache git

# Create app user
RUN addgroup -g 1001 -S nodejs && \
    adduser -S aidev -u 1001

# Set working directory
WORKDIR /app

# Copy built application
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./

# Create necessary directories
RUN mkdir -p /app/config /app/logs && \
    chown -R aidev:nodejs /app

# Switch to app user
USER aidev

# Set environment variables
ENV NODE_ENV=production
ENV AI_DEV_CONFIG_PATH=/app/config

# Expose port (if needed for future API features)
EXPOSE 3000

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD node -e "console.log('Health check passed')" || exit 1

# Default command
ENTRYPOINT ["node", "dist/cli/index.js"]
CMD ["--help"]
