# Build stage - optimized for production
FROM node:18-alpine AS builder

WORKDIR /sc600-admin

# Copy package files first for better caching
COPY package*.json ./
COPY .npmrc ./

# Install dependencies
RUN npm install --no-audit --no-fund && \
    npm cache clean --force

# Copy source files
COPY . .

# Build the application
RUN npm run build

# Production stage - copy built files to volume
FROM alpine:latest

# Install bash for any scripts that might need it
RUN apk add --no-cache bash

# Create app directory and non-root user
RUN addgroup -g 1001 -S appuser && \
    adduser -S appuser -u 1001
    
WORKDIR /app

# Copy built assets from build stage (note: source is /sc600-admin/dist)
COPY --from=builder /sc600-admin/dist /app/dist

# Change ownership to non-root user
RUN chown -R appuser:appuser /app

USER appuser

# Keep container running to maintain volume access
CMD ["tail", "-f", "/dev/null"]
