FROM node:22-bookworm-slim AS build

WORKDIR /app
COPY scripts/src/server_ts/package*.json ./scripts/src/server_ts/
WORKDIR /app/scripts/src/server_ts
RUN npm ci

WORKDIR /app
COPY . .
WORKDIR /app/scripts/src/server_ts
RUN npm run build

FROM node:22-bookworm-slim AS runtime

ENV NODE_ENV=production

WORKDIR /app
COPY --from=build /app/config.json ./config.json
COPY --from=build /app/scripts/src/server_ts/package*.json ./scripts/src/server_ts/
COPY --from=build /app/scripts/src/server_ts/dist ./scripts/src/server_ts/dist
COPY --from=build /app/unidesk-adapter.cjs ./scripts/src/server_ts/unidesk-adapter.cjs

WORKDIR /app/scripts/src/server_ts
RUN npm ci --omit=dev && npm cache clean --force

CMD ["node", "unidesk-adapter.cjs"]
