# yugioh-legacy-bots: the library, both arenas and the tests, on the engine release engine.json pins. There is no
# dotnet on the build hosts; everything builds here. Build context: the repo root.
#   docker build --target test .                 build, then the tests (the Linux arena plays the fixture duels)
#   docker build --target dist -o out .          the same, then out/{lib,bot-arena-linux-x64,bot-arena-win-x64}
#   docker build --target release -o dist .      the same, packed as the release assets (.gitea/workflows/ci.yml)
#   docker build --build-arg BOTS_VERSION=v0.1.0 ...   stamps the version (default: local)

ARG ENGINE_RELEASES=https://gitea.dev.blueoceanswim.com/donny/yugioh-legacy-engine/releases/download

# The engine is never built here: build/fetch-engine.sh downloads the pinned release and refuses a bundle whose
# engine.json is not the pin.
FROM debian:bookworm-slim@sha256:3783cc01769c7b2b1b83a5c5ad96c815348e28ed7da68e2e3687004faa906251 AS engine
ARG ENGINE_RELEASES
RUN apt-get update \
    && apt-get install -y --no-install-recommends ca-certificates curl jq unzip zip \
    && rm -rf /var/lib/apt/lists/*
COPY engine.json /repo/engine.json
COPY build/fetch-engine.sh /repo/build/fetch-engine.sh
RUN ENGINE_RELEASES=$ENGINE_RELEASES /repo/build/fetch-engine.sh /engine

FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:78235e09001f52b6592c458ac010775ebac6725422e80cd0c1650590f67b2743 AS build
ARG BOTS_VERSION=local
ENV DOTNET_CLI_TELEMETRY_OPTOUT=1 DOTNET_NOLOGO=1
WORKDIR /src
COPY --from=engine /engine/ .engine/
COPY Directory.Build.props engine.json ./
COPY src/YGOLegacy.Bots/YGOLegacy.Bots.csproj src/YGOLegacy.Bots/
COPY src/BotArena/BotArena.csproj src/BotArena/
COPY tests/YGOLegacy.Bots.Tests/YGOLegacy.Bots.Tests.csproj tests/YGOLegacy.Bots.Tests/
RUN for rid in linux-x64 win-x64; do dotnet restore src/BotArena -r $rid -p:SelfContained=true -p:RestoreUseStaticGraphEvaluation=false || exit 1; done \
    && dotnet restore tests/YGOLegacy.Bots.Tests
COPY src/ src/
COPY tests/ tests/
RUN dotnet build src/YGOLegacy.Bots -c Release --no-restore -p:BotsVersion=$BOTS_VERSION -warnaserror:CS8032 \
    && mkdir -p /out/lib \
    && cp src/YGOLegacy.Bots/bin/Release/netstandard2.1/YGOLegacy.Bots.dll src/YGOLegacy.Bots/bin/Release/netstandard2.1/YGOLegacy.Bots.pdb /out/lib/ \
    && for rid in linux-x64 win-x64; do \
         dotnet publish src/BotArena -c Release -r $rid --self-contained -p:BotsVersion=$BOTS_VERSION \
           -o /out/bot-arena-$rid || exit 1; \
       done \
    && ls -l /out/lib /out/bot-arena-linux-x64/BotArena /out/bot-arena-linux-x64/libygol-engine.so /out/bot-arena-win-x64/BotArena.exe /out/bot-arena-win-x64/ygol-engine.dll

# The tests: the library through BotHost, and the published Linux arena on tests/fixture/game.
FROM build AS test
ARG BOTS_VERSION=local
ENV BOTARENA_EXE=/out/bot-arena-linux-x64/BotArena BOTS_VERSION=$BOTS_VERSION
RUN dotnet test tests/YGOLegacy.Bots.Tests -c Release --no-restore -p:BotsVersion=$BOTS_VERSION --logger "console;verbosity=normal"

FROM scratch AS dist
COPY --from=test /out/ /

# The release assets: each bundle carries bots.json { version, engine }, and so does the release itself.
FROM engine AS release-pack
ARG BOTS_VERSION=local
COPY --from=dist / /out/
COPY engine.json /repo/engine.json
RUN set -eu; cd /out \
    && jq -n --arg version "$BOTS_VERSION" --arg engine "$(jq -r .version /repo/engine.json)" \
         '{version: $version, engine: $engine}' > bots.json \
    && for d in lib bot-arena-linux-x64 bot-arena-win-x64; do cp bots.json "$d/"; done \
    && mkdir /dist \
    && (cd lib && zip -q -X /dist/bots-lib.zip YGOLegacy.Bots.dll YGOLegacy.Bots.pdb bots.json) \
    && tar czf /dist/bot-arena-linux-x64.tar.gz bot-arena-linux-x64 \
    && zip -q -r -X /dist/bot-arena-win-x64.zip bot-arena-win-x64 \
    && cp bots.json /dist/ \
    && cat /dist/bots.json && ls -l /dist

FROM scratch AS release
COPY --from=release-pack /dist/ /
