FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

# Copy project files first for restore layer
COPY ReserveManagementSystemUI.csproj ./source/

# Optional credential provider for Azure Artifacts (matches API pattern)
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/*

ADD https://github.com/microsoft/artifacts-credprovider/releases/latest/download/Microsoft.Net8.NuGet.CredentialProvider.tar.gz /tmp/
# Extract and install Credential Provider
RUN mkdir -p /root/.nuget/plugins/netcore && \
    tar --strip-components=3 -xzf /tmp/Microsoft.Net8.NuGet.CredentialProvider.tar.gz -C /root/.nuget/plugins/netcore && \
    rm /tmp/Microsoft.Net8.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}\"}]}"

# Restore
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/ReserveManagementSystemUI.csproj

# Copy all sources and publish
COPY . ./source
WORKDIR /source
RUN dotnet publish -c Release -o /app/publish

# Runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine
RUN apk add --no-cache icu-libs && apk add --no-cache tzdata;
ENV TZ=America/Chicago
ENV LANG=en_US.UTF-8
ENV LC_ALL=en_US.UTF-8
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENV ASPNETCORE_URLS=http://+:80

WORKDIR /app
COPY --from=build /app/publish .
EXPOSE 80
ENTRYPOINT ["dotnet", "ReserveManagementSystemUI.dll"]
