# syntax=docker/dockerfile:1.7
#
# Multi-stage build for Prospect.Server.Api targeting linux-x64.
#
# Build args:
#   PROSPECT_COMMIT   Source commit to check out from deiteris/Prospect
#                     (default: a0a27254ad18c79e8ed50b8b5378a39e141f76e4,
#                      current deiteris Season 2-compatible baseline)
#   BUILD_CONFIG      MSBuild configuration name (default: "Season 2 Release";
#                     "Season 3 Release" is the other valid value. Plain
#                     "Release" fails with #error: 'Unsupported build type').
#   SLURPNET_REVISION Exact 40-character SlurpNet commit used for this image.
#   SLURPNET_SOURCE_OFFER
#                     HTTPS URL for the immutable Corresponding Source archive.
#                     Production preflight rejects the local placeholder.

ARG PROSPECT_COMMIT=a0a27254ad18c79e8ed50b8b5378a39e141f76e4
ARG BUILD_CONFIG="Season 2 Release"
ARG SLURPNET_REVISION=0000000000000000000000000000000000000000
ARG SLURPNET_SOURCE_OFFER=https://mods.slurpgg.net/source/the-last-frontier/unpublished/prospect-corresponding-source.tar.gz

FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:89ce6291bde9acdf59594e79fb8277c6d84c46e4b1f5bf126a4f18766e4bd597 AS build
ARG PROSPECT_COMMIT
ARG BUILD_CONFIG
WORKDIR /src

# Clone at the pinned commit. Doing this inside the build (rather than COPY .)
# means the Dockerfile is self-contained and the build context stays tiny.
RUN git init --quiet . \
    && git config user.email "build@slurpnet.local" \
    && git config user.name "slurpnet build" \
    && git remote add origin https://github.com/deiteris/Prospect.git \
    && git fetch --depth 1 --quiet origin "${PROSPECT_COMMIT}" \
    && git checkout --quiet FETCH_HEAD

# Slurpnet patches layered on top of the pinned upstream. The patches live
# in server/patches/*.patch and are applied in lexical order. Bumping
# PROSPECT_COMMIT may require rebasing them.
COPY patches/ /tmp/patches/
RUN set -e; \
    if ls /tmp/patches/*.patch >/dev/null 2>&1; then \
      for p in /tmp/patches/*.patch; do \
        echo "Applying $p"; \
        git update-index --refresh; \
        git apply --check --index --unidiff-zero "$p"; \
        git apply --index --whitespace=error --unidiff-zero "$p"; \
      done; \
    fi

RUN dotnet restore src/Prospect.Server.Api/Prospect.Server.Api.csproj \
    -r linux-x64

RUN dotnet publish src/Prospect.Server.Api/Prospect.Server.Api.csproj \
    -c "${BUILD_CONFIG}" \
    -r linux-x64 \
    --self-contained false \
    --no-restore \
    -o /out

# ---- runtime stage ----
FROM mcr.microsoft.com/dotnet/aspnet:8.0@sha256:9822a6201c64c9f38b8a190832a7ae21cd30150d509b3ea170fd83b4f4e6166a
ARG PROSPECT_COMMIT
ARG BUILD_CONFIG
ARG SLURPNET_REVISION
ARG SLURPNET_SOURCE_OFFER

LABEL org.opencontainers.image.licenses="AGPL-3.0-only" \
      org.opencontainers.image.source="https://gitlab.slurpgg.net/slurpnet/servers/slurpnet-thelastfrontier" \
      org.opencontainers.image.revision="${SLURPNET_REVISION}" \
      net.slurpgg.prospect.revision="${PROSPECT_COMMIT}" \
      net.slurpgg.prospect.build-configuration="${BUILD_CONFIG}" \
      net.slurpgg.source.offer="${SLURPNET_SOURCE_OFFER}"

WORKDIR /app

# curl is needed by the compose healthcheck. aspnet:8.0 ships without wget
# or curl, so the previous healthcheck recipe failed with "wget: not found"
# on every probe (cosmetic — the API itself was healthy).
RUN apt-get update \
    && apt-get install -y --no-install-recommends curl \
    && rm -rf /var/lib/apt/lists/*

COPY --from=build /out/ /app/
COPY legal/ /usr/share/doc/slurpnet-thelastfrontier/
COPY write-source-manifest.sh /usr/local/bin/write-source-manifest
COPY patches/ /tmp/slurpnet-patches/
RUN chmod 0555 /usr/local/bin/write-source-manifest \
    && /usr/local/bin/write-source-manifest \
      /usr/share/doc/slurpnet-thelastfrontier/SOURCE.txt \
      "${SLURPNET_REVISION}" \
      "${SLURPNET_SOURCE_OFFER}" \
      "${PROSPECT_COMMIT}" \
      "${BUILD_CONFIG}" \
      /tmp/slurpnet-patches \
    && chmod 0444 \
      /usr/share/doc/slurpnet-thelastfrontier/LICENSE \
      /usr/share/doc/slurpnet-thelastfrontier/THIRD_PARTY_NOTICES.md \
    && rm -rf /usr/local/bin/write-source-manifest /tmp/slurpnet-patches

# Default ports — overridable via ASPNETCORE_URLS or Kestrel:Endpoints
EXPOSE 8000 8443

# Don't run as root in production. ASP.NET base image already provides "app".
USER app

ENTRYPOINT ["dotnet", "Prospect.Server.Api.dll"]
