# Stage 1: Build the application
FROM node:24-alpine AS builder
WORKDIR /app

# Install dependencies first for better Docker layer caching.
COPY package*.json ./
RUN npm install

# Copy the rest of the source code.
COPY . .

# Build Argument to determine build environment (staging, prod, or production).
ARG BUILD_MODE=staging
ENV NODE_ENV=production

# Sync the SvelteKit project to generate ./.svelte-kit/tsconfig.json
RUN npx svelte-kit sync

# Perform the build based on the BUILD_MODE argument.
RUN if [ "$BUILD_MODE" = "prod" ] || [ "$BUILD_MODE" = "production" ]; then \
        npm run build:prod; \
    else \
        npm run build:staging; \
    fi

# Stage 2: Final runtime image
FROM node:24-alpine AS deploy-node
WORKDIR /app

# Copy built files and package info.
COPY --from=builder /app/build .
COPY --from=builder /app/package.json .
COPY --from=builder /app/package-lock.json .

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

# Copy the resulting .env.production file to .env.
COPY --from=builder /app/.env.production .env

# SvelteKit (via adapter-node) defaults to port 3000.
EXPOSE 3000

# Healthcheck to verify the app is running
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD node -e "fetch('http://localhost:3000/health').then(r => r.ok ? process.exit(0) : process.exit(1)).catch(() => process.exit(1))"

CMD ["node", "index.js"]
