# Use a multi-stage build to optimize the final image size

# Stage 1: Prepare the pruned workspace
FROM node:24-alpine AS prepare

WORKDIR /app

RUN npm install -g pnpm@10.28.1
RUN npm install -g turbo@2.7.5

COPY . .

# Prune the workspace to only include what's needed for the web service
RUN pnpm turbo prune biostacker-frontend --docker

# Stage 2: Build the application
FROM node:24-alpine AS builder

WORKDIR /app

RUN npm install -g pnpm@10.28.1
RUN npm install -g turbo@2.7.5

# First install the dependencies (as they change less often)
COPY --from=prepare /app/out/json/ .
RUN pnpm install --frozen-lockfile

# Build the project
COPY --from=prepare /app/out/full/ .

RUN pwd
RUN ls -la /app
COPY --from=prepare /app/tsconfig.base.json .

RUN pnpm build
RUN ls -la /app

# Stage 3: Create the production image with Caddy as reverse proxy
FROM caddy:2-alpine AS runner

WORKDIR /usr/share/caddy

# Copy the built assets from the builder stage
COPY --from=builder /app/frontend/dist ./dist

# Copy Caddyfile for configuration
COPY --from=builder /app/frontend/Caddyfile ./Caddyfile

EXPOSE 8667

CMD ["caddy", "run", "--config", "/usr/share/caddy/Caddyfile", "--adapter", "caddyfile"]
