# Build stage
FROM node:20-alpine AS builder

# Cache buster - wijzig BUILD_DATE in docker-compose om rebuild te forceren
ARG BUILD_DATE=unknown
RUN echo "Build date: $BUILD_DATE"

# Install build dependencies for better-sqlite3
RUN apk add --no-cache python3 make g++

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install all dependencies (including devDependencies for build)
RUN npm install

# Copy source code
COPY . .

# Build the application
RUN npm run build

# Compile server TypeScript
RUN npx tsc -p tsconfig.server.json

# Production stage
FROM node:20-alpine

# Install build dependencies for better-sqlite3
RUN apk add --no-cache python3 make g++

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install production dependencies only
RUN npm install --omit=dev

# Copy built files from builder
COPY --from=builder /app/dist ./dist

# Create data directory
RUN mkdir -p /app/data

# Expose port
EXPOSE 4000

# Set environment variables
ENV NODE_ENV=production
ENV PORT=4000
ENV SESSION_SECRET=change-this-in-production
ENV DB_PATH=/app/data/questionnaire.db

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