ARG DOTNET_SDK_IMAGE=mcr.m.daocloud.io/dotnet/sdk:8.0-bookworm-slim
ARG DOTNET_RUNTIME_IMAGE=mcr.m.daocloud.io/dotnet/aspnet:8.0-bookworm-slim

FROM --platform=$BUILDPLATFORM ${DOTNET_SDK_IMAGE} AS build
WORKDIR /src

# Keep proxy settings available for any package endpoint fallback. The primary
# NuGet source is pinned to the domestic mirror in the repository NuGet.config.
ARG HTTP_PROXY
ARG HTTPS_PROXY
ARG NO_PROXY
ARG http_proxy
ARG https_proxy
ARG no_proxy
ARG BUILDPLATFORM
ARG TARGETARCH
ENV HTTP_PROXY=${HTTP_PROXY} \
    HTTPS_PROXY=${HTTPS_PROXY} \
    NO_PROXY=${NO_PROXY} \
    http_proxy=${http_proxy} \
    https_proxy=${https_proxy} \
    no_proxy=${no_proxy}

# Map Docker TARGETARCH to .NET RID so native dependencies (SQLite, EF Core)
# resolve to the correct platform. Framework-dependent output — apphost stays
# at /p:UseAppHost=false.
COPY . .

# Run the SDK on the builder's native platform and cross-publish for the target
# architecture. This keeps restore/publish out of QEMU for ARM64 builds.
RUN --mount=type=cache,id=liverecorder-nuget,target=/root/.nuget/packages,sharing=locked \
    case "$TARGETARCH" in \
      amd64) RID=linux-x64 ;; \
      arm64) RID=linux-arm64 ;; \
      *) echo "ERROR: unknown TARGETARCH=$TARGETARCH" >&2; exit 1 ;; \
    esac ; \
    echo "Building on BUILDPLATFORM=$BUILDPLATFORM for TARGETARCH=$TARGETARCH -> RID=$RID" ; \
    restore_attempt=1 ; \
    until dotnet restore "src/LiveRecorder.WebApi/LiveRecorder.WebApi.csproj" \
      -r "$RID" -p:RestoreUseStaticGraphEvaluation=true --disable-parallel; do \
      if [ "$restore_attempt" -ge 3 ]; then \
        echo "ERROR: dotnet restore failed after $restore_attempt attempts for RID=$RID" >&2 ; \
        exit 1 ; \
      fi ; \
      restore_attempt=$((restore_attempt + 1)) ; \
      echo "dotnet restore failed; retrying attempt $restore_attempt/3 in 5 seconds" >&2 ; \
      sleep 5 ; \
    done ; \
    dotnet publish "src/LiveRecorder.WebApi/LiveRecorder.WebApi.csproj" \
      -c Release -o /app/publish -r "$RID" --no-restore \
      /p:UseAppHost=false /p:DebugType=None /p:DebugSymbols=false /maxcpucount:1

FROM ${DOTNET_RUNTIME_IMAGE} AS runtime

RUN sed -i \
      -e 's|http://deb.debian.org|https://mirrors.huaweicloud.com|g' \
      -e 's|https://deb.debian.org|https://mirrors.huaweicloud.com|g' \
      /etc/apt/sources.list.d/debian.sources \
    && apt-get update -o Acquire::ForceIPv4=true -o Acquire::Retries=5 \
    && apt-get install -o Acquire::ForceIPv4=true -o Acquire::Retries=5 -y --no-install-recommends ffmpeg nodejs ca-certificates curl jq \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production
ENV ConnectionStrings__DefaultConnection=Host=postgres;Port=5432;Database=live_recorder;Username=postgres;Password=postgres

COPY --from=build /app/publish ./

RUN mkdir -p /app/data /app/records

VOLUME ["/app/data", "/app/records"]
EXPOSE 8080

ENTRYPOINT ["dotnet", "LiveRecorder.WebApi.dll"]
