# Builds the LaDOSE.DiscordBot image. Context is LaDOSE.Src, same as LaDOSE.Src/Dockerfile,
# so the two share LaDOSE.Src/.dockerignore — hence the LaDOSE.DiscordBot/ prefix on every
# COPY below. docker-compose.yml passes `dockerfile: LaDOSE.DiscordBot/Dockerfile`.
#
# The context has to be the whole of LaDOSE.Src, not this directory: the bot pulls in
# LaDOSE.REST -> LaDOSE.DTO by ProjectReference, and Libraries/ChallongeCSharpDriver.dll
# by HintPath.
ARG DOTNET_VERSION=9.0

FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION} AS build
WORKDIR /src

# Release here, unlike the API image. Nothing in this project is gated on Debug — the
# #if DEBUG / Condition="'$(Configuration)' == 'Debug'" pairs that force the API to build
# Debug (OpenAPI, Scalar) have no equivalent in LaDOSE.DiscordBot.csproj.
ARG BUILD_CONFIGURATION=Release

# Project files first so this layer survives every .cs edit. Restore under the same
# Configuration as the publish below, matching LaDOSE.Src/Dockerfile.
COPY global.json ./
COPY LaDOSE.DiscordBot/LaDOSE.DiscordBot.csproj LaDOSE.DiscordBot/
COPY LaDOSE.REST/LaDOSE.REST.csproj LaDOSE.REST/
COPY LaDOSE.DTO/LaDOSE.DTO.csproj LaDOSE.DTO/
RUN dotnet restore LaDOSE.DiscordBot/LaDOSE.DiscordBot.csproj -p:Configuration=${BUILD_CONFIGURATION}

COPY . .
RUN dotnet publish LaDOSE.DiscordBot/LaDOSE.DiscordBot.csproj -c ${BUILD_CONFIGURATION} --no-restore -o /app/out

# The file on disk is Quotes.txt; Command/Public.cs:114 opens "quotes.txt". Windows does
# not care, Linux does — without this the !Quote command throws FileNotFoundException
# (LoadQuote, unlike LoadCards, does not catch it). A copy rather than a rename, so the
# name the .csproj lists keeps working too.
RUN if [ -f /app/out/Quotes.txt ] && [ ! -f /app/out/quotes.txt ]; then \
        cp /app/out/Quotes.txt /app/out/quotes.txt; \
    fi

# runtime, not aspnet: this is a console app with no Kestrel and no listening port.
FROM mcr.microsoft.com/dotnet/runtime:${DOTNET_VERSION}
WORKDIR /app

COPY --from=build /app/out/ ./
COPY LaDOSE.DiscordBot/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

# WORKDIR is load-bearing, not cosmetic: Program.cs points its ConfigurationBuilder at
# Directory.GetCurrentDirectory() for settings.json, and Public.cs reads questions.txt /
# answers.txt / quotes.txt by bare relative path. All four sit in /app.
#
# The entrypoint renders settings.json from the environment before handing over; see the
# script for why that indirection exists.
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["dotnet", "LaDOSE.DiscordBot.dll"]
