# Flashcard — single-container production image.
# Runs the Express API and serves the built React frontend on PORT (default 3000).
#
# The backend is launched with tsx (TypeScript runtime) because the shared
# package is consumed as TypeScript source; this matches how the app runs in dev
# and avoids a separate compile step for the shared workspace.
FROM node:22-bookworm-slim

# better-sqlite3 is a native module. Provide a toolchain in case no prebuilt
# binary exists for the target platform (most amd64/arm64 hosts use a prebuild).
RUN apt-get update \
  && apt-get install -y --no-install-recommends python3 make g++ ca-certificates \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install dependencies first for better layer caching.
# Do NOT set NODE_ENV=production here: the image needs devDependencies
# (vite to build the frontend, tsx to run the backend).
COPY package.json package-lock.json ./
COPY packages/shared/package.json packages/shared/package.json
COPY packages/backend/package.json packages/backend/package.json
COPY packages/frontend/package.json packages/frontend/package.json
RUN npm ci

# Copy the source and build the static frontend (served by the backend).
COPY . .
RUN npm -w @flashcard/frontend run build \
  && chmod +x /app/docker-entrypoint.sh

EXPOSE 3000
ENTRYPOINT ["/app/docker-entrypoint.sh"]
