# Build context: repo root (Yonexus/)
# ── Stage 1: compile ──────────────────────────────────────────────────────────
FROM node:22-alpine AS builder

WORKDIR /build

# Server imports Yonexus.Protocol and Yonexus.Client/crypto — all needed for tsc
COPY Yonexus.Protocol/src                    ./Yonexus.Protocol/src
COPY Yonexus.Client/plugin/crypto            ./Yonexus.Client/plugin/crypto

COPY Yonexus.Server/package.json             ./Yonexus.Server/
COPY Yonexus.Server/package-lock.json        ./Yonexus.Server/
COPY Yonexus.Server/tsconfig.json            ./Yonexus.Server/
COPY Yonexus.Server/plugin                   ./Yonexus.Server/plugin

WORKDIR /build/Yonexus.Server
RUN npm ci
RUN npm run build

# ── Stage 2: runtime ─────────────────────────────────────────────────────────
FROM node:22-alpine AS runtime

RUN npm install -g openclaw@2026.4.9

WORKDIR /app

# Layout expected by install.mjs: repoRoot = /app, sourceDist = /app/dist
COPY --from=builder /build/Yonexus.Server/dist         ./dist
COPY --from=builder /build/Yonexus.Server/node_modules ./node_modules
COPY Yonexus.Server/package.json               ./package.json
COPY Yonexus.Server/plugin/openclaw.plugin.json ./plugin/openclaw.plugin.json
COPY Yonexus.Server/scripts/install.mjs        ./scripts/install.mjs

COPY tests/docker/server-test-plugin /app/server-test-plugin
COPY tests/docker/server/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

EXPOSE 8787

ENTRYPOINT ["/entrypoint.sh"]
