FROM node:24-alpine AS builder
WORKDIR /app

RUN npm install -g pnpm@10.26.1

# 1. Alle Projektdateien in den Container bringen
COPY . .

# 2. Abhängigkeiten installieren
RUN pnpm install --frozen-lockfile

# Zwingt pnpm, alle nativen Binärdateien für Alpine physisch bereitzustellen
RUN pnpm add -w @rollup/rollup-linux-x64-musl lightningcss-linux-x64-musl @tailwindcss/oxide-linux-x64-musl --save-optional --config.ignore-scripts=false

ENV BASE_PATH=/
ENV PORT=8080

# HIER DER FIX: Wir kompilieren das gesamte Monorepo (inklusive lib/db), 
# damit überall die /dist Ordner für den Runner entstehen.
RUN pnpm -r --if-present run build

FROM node:24-alpine AS runner
WORKDIR /app

RUN npm install -g pnpm@10.26.1

COPY pnpm-lock.yaml pnpm-workspace.yaml package.json ./
COPY lib/db/package.json lib/db/
COPY lib/api-spec/package.json lib/api-spec/
COPY lib/api-zod/package.json lib/api-zod/
COPY lib/api-client-react/package.json lib/api-client-react/
COPY artifacts/api-server/package.json artifacts/api-server/
COPY artifacts/toolrate/package.json artifacts/toolrate/

RUN pnpm install --frozen-lockfile --prod

# Auch für den Runner-Stage stellen wir sicher, dass alle drei Pakete im Produktiv-Image existieren
RUN pnpm add -w @rollup/rollup-linux-x64-musl lightningcss-linux-x64-musl @tailwindcss/oxide-linux-x64-musl --save-optional --prod

COPY --from=builder /app/lib/db/dist lib/db/dist
COPY --from=builder /app/artifacts/api-server/dist artifacts/api-server/dist
COPY --from=builder /app/artifacts/toolrate/dist artifacts/toolrate/dist

ENV NODE_ENV=production
ENV STATIC_DIR=/app/artifacts/toolrate/dist/public

EXPOSE 8080

CMD ["node", "--enable-source-maps", "artifacts/api-server/dist/index.mjs"]
