# Dockerfile

# Stage 1: Build the React application
FROM node:22-alpine AS build

LABEL stage="build-local-testing"

WORKDIR /app

# Copy root package.json/lockfile plus workspace manifests so npm can
# resolve the @ttrpg/shared and server workspace links during install.
COPY package*.json ./
COPY shared/package.json ./shared/
COPY server/package.json ./server/

# Install dependencies using the lock file for consistency
RUN npm install

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

# --- For Local Testing with .env.local ---
# Copy your .env.local file as .env in the build context.
# Create React App's build script will automatically load variables from this .env file.
# IMPORTANT: Ensure .env.local contains your actual Firebase API keys and other secrets.
# This .env.local file MUST be in your .gitignore and NOT committed to your repository.
# This Docker image, built this way, CONTAINS YOUR SECRETS and should NOT be pushed to a public registry.
COPY .env.local .env
# --- End Local Testing Section ---

# Build the application. react-scripts build will use environment variables
# prefixed with REACT_APP_ (either from the .env file copied above or from the build environment).
# Set NODE_OPTIONS to use the legacy OpenSSL provider for the build step.
RUN NODE_OPTIONS=--openssl-legacy-provider npm run build

# Stage 2: Serve the static files using Nginx
FROM nginx:1.25-alpine

LABEL stage="nginx-server"

# Copy the build output from the 'build' stage to Nginx's html directory
COPY --from=build /app/build /usr/share/nginx/html

# Replace default nginx config with one that handles SPA client-side routing
COPY nginx-docker.conf /etc/nginx/conf.d/default.conf

# Expose port 80 (Nginx default)
EXPOSE 80

# Start Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]
