# Use the official Node.js 18 slim image as base
FROM node:18-slim

# Set working directory
WORKDIR /app

# Create necessary directories with proper permissions
RUN mkdir -p /app/.next /app/logs /app/node_modules
RUN chmod 777 /app/.next /app/logs

# Copy package files
COPY package*.json ./

# Install all dependencies
RUN npm install

# Copy application code
COPY . .

# Expose port
EXPOSE 3000

# Create non-root user for security
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
RUN chown -R nextjs:nodejs /app

USER nextjs

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

# Run the development server
CMD ["npm", "run", "dev"]