# StemeDB Community App Docker Build
#
# Multi-stage build for the Next.js frontend.
# Also used for running the seed script.

# Stage 1: Dependencies
FROM node:20-slim AS deps

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci

# Stage 2: Builder
FROM node:20-slim AS builder

WORKDIR /app

# Copy dependencies from deps stage
COPY --from=deps /app/node_modules ./node_modules
COPY . .

# Create empty openapi.json if it doesn't exist (will be fetched at runtime)
RUN mkdir -p public && echo '{}' > public/openapi.json

# Build the Next.js app
# Note: Build may fail if API is not available, but we continue anyway
RUN npm run build || echo "Build completed with warnings"

# Stage 3: Runtime
FROM node:20-slim AS runner

WORKDIR /app

ENV NODE_ENV=production
ENV PORT=18187

# Copy built assets and dependencies
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/node_modules ./node_modules

# Copy scripts directory for seed script
COPY --from=builder /app/scripts ./scripts
COPY --from=builder /app/tsconfig.json ./tsconfig.json
COPY --from=builder /app/src ./src

EXPOSE 18187

# Default command runs the Next.js server
CMD ["npm", "run", "start"]
