# Auto-generated Dockerfile for Node.js application
# Generated by BYOP Engine - Node.js Stack Analyzer

FROM node:{{.NodeVersion}}-alpine

# Set working directory
WORKDIR /app

{{if .SystemDeps}}
# Install system dependencies
RUN apk add --no-cache {{.SystemDeps | join " "}}
{{end}}

# Copy package files for better dependency caching
COPY package.json ./
{{if .HasYarnLock}}
COPY yarn.lock ./
{{else if .HasPnpmLock}}
COPY pnpm-lock.yaml ./
{{else if .HasPackageLock}}
COPY package-lock.json ./
{{end}}

# Install dependencies based on package manager
{{if eq .PackageManager "yarn"}}
RUN yarn install --frozen-lockfile {{if .ProductionOnly}}--production{{end}}
{{else if eq .PackageManager "pnpm"}}
RUN corepack enable && pnpm install --frozen-lockfile {{if .ProductionOnly}}--prod{{end}}
{{else}}
{{if .ProductionOnly}}
RUN npm ci --only=production
{{else}}
RUN npm ci
{{end}}
{{end}}

# Copy source code
COPY . .

{{if .HasBuildScript}}
# Build the application
{{if eq .PackageManager "yarn"}}
RUN yarn build
{{else if eq .PackageManager "pnpm"}}
RUN pnpm build
{{else}}
RUN npm run build
{{end}}
{{end}}

{{if .PruneDevDeps}}
# Remove development dependencies after build
{{if eq .PackageManager "yarn"}}
RUN yarn install --frozen-lockfile --production && yarn cache clean
{{else if eq .PackageManager "pnpm"}}
RUN pnpm prune --prod
{{else}}
RUN npm prune --production
{{end}}
{{end}}

# Create non-root user for security
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nodeuser -u 1001 && \
    chown -R nodeuser:nodejs /app

# Switch to non-root user
USER nodeuser

# Expose port
EXPOSE {{.Port}}

{{if .HealthCheckEndpoint}}
# Add health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://localhost:{{.Port}}{{.HealthCheckEndpoint}} || exit 1
{{end}}

# Start the application
CMD {{.StartCommand | toJSON}}
