# HoofScan Swiss-Galoppers Production Docker Image
# Optimiert für Enterprise-Deployment

FROM node:20-alpine AS base

# Install system dependencies
RUN apk add --no-cache \
    postgresql-client \
    curl \
    bash \
    && rm -rf /var/cache/apk/*

# 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 . .

# Build application
RUN npm run build

# Create non-root user for security
RUN addgroup -g 1001 -S hoofscan && \
    adduser -S hoofscan -u 1001

# Create necessary directories with proper permissions
RUN mkdir -p /app/logs /app/backups /app/uploads && \
    chown -R hoofscan:hoofscan /app

# Switch to non-root user
USER hoofscan

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

# Expose port
EXPOSE 5000

# Environment variables
ENV NODE_ENV=production
ENV PORT=5000

# Start application
CMD ["node", "dist/server/index.js"]