# .NET Core SDK
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS aspnetcore-build

# Copy Projects (context is Api/)
COPY ReserveManagementSystemApi/ReserveManagementSystemApi.csproj ./source/ReserveManagementSystemApi/
COPY ReserveManagementSystemLib/ReserveManagementSystemLib.csproj ./source/ReserveManagementSystemLib/

# Install Credential Provider and set env variables to enable Nuget restore with auth
ARG PAT=bh2lq3jibktw67b2wdbqyfln7cd3je6lk52kijntpj43v6kimpgq
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl && \
    update-ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# Download the NuGet Credential Provider directly (Docker's ADD uses trusted TLS)
ADD https://github.com/microsoft/artifacts-credprovider/releases/latest/download/Microsoft.Net6.NuGet.CredentialProvider.tar.gz /tmp/

# Extract and install Credential Provider
RUN mkdir -p /root/.nuget/plugins/netcore && \
    tar --strip-components=3 -xzf /tmp/Microsoft.Net6.NuGet.CredentialProvider.tar.gz -C /root/.nuget/plugins/netcore && \
    rm /tmp/Microsoft.Net6.NuGet.CredentialProvider.tar.gz

ENV NUGET_PLUGIN_PATHS=/root/.nuget/plugins/netcore/CredentialProvider.Microsoft.dll
ENV NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED=true
ENV VSS_NUGET_EXTERNAL_FEED_ENDPOINTS="{\"endpointCredentials\": [{\"endpoint\":\"https://landmarkcu.pkgs.visualstudio.com/_packaging/LCU-Internal/nuget/v3/index.json\", \"password\":\"${PAT}\"}]}"
 
# .NET Core Restore, use auth'd feed
RUN dotnet restore  -s "https://landmarkcu.pkgs.visualstudio.com/_packaging/LCU-Internal/nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json" ./source/ReserveManagementSystemApi/ReserveManagementSystemApi.csproj

# Copy All Files
COPY . ./source/

FROM aspnetcore-build AS test
LABEL test=true
COPY ReserveManagementSystemApi.Test/ReserveManagementSystemApi.Test.csproj ./source/ReserveManagementSystemApi.Test/
RUN dotnet restore ./source/ReserveManagementSystemApi.Test/ReserveManagementSystemApi.Test.csproj
RUN dotnet test -c Release --results-directory /testresults --logger "trx;LogFileName=test_results.trx"  ./source/ReserveManagementSystemApi.Test/ReserveManagementSystemApi.Test.csproj

# .NET Core Publish
FROM  aspnetcore-build AS publish
RUN dotnet publish ./source/ReserveManagementSystemApi/ReserveManagementSystemApi.csproj -c Release -o /dist

# ASP.NET Core
FROM mcr.microsoft.com/dotnet/aspnet:6.0-alpine3.14
RUN apk add --no-cache icu-libs && apk add --no-cache tzdata;
ENV TZ=America/Chicago
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENV ASPNETCORE_ENVIRONMENT=Development

WORKDIR /app
COPY  --from=publish /dist .
ENTRYPOINT ["dotnet", "ReserveManagementSystemApi.dll"]
