# Build stage
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build

ARG PAT=bh2lq3jibktw67b2wdbqyfln7cd3je6lk52kijntpj43v6kimpgq

WORKDIR /source

# Install credential provider for authenticated Azure DevOps feeds
# Debian 10 (buster) is archived; repoint sources to archive.debian.org before installing packages.
RUN sed -i 's|deb.debian.org/debian|archive.debian.org/debian|g; s|security.debian.org/debian-security|archive.debian.org/debian-security|g; s|deb.debian.org/debian-security|archive.debian.org/debian-security|g' /etc/apt/sources.list && \
    apt-get update && apt-get install -y --no-install-recommends ca-certificates curl unzip && \
    update-ca-certificates && \
    rm -rf /var/lib/apt/lists/*

# Configure NuGet with Azure DevOps feed credentials (avoids plugin dependency)
RUN mkdir -p /root/.nuget/NuGet && \
    cat > /root/.nuget/NuGet/NuGet.Config <<EOF
<configuration>
  <packageSources>
    <clear />
    <add key="LCU-Internal" value="https://landmarkcu.pkgs.visualstudio.com/_packaging/LCU-Internal/nuget/v3/index.json" />
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
  <packageSourceCredentials>
    <LCU-Internal>
      <add key="Username" value="pat" />
      <add key="ClearTextPassword" value="${PAT}" />
    </LCU-Internal>
  </packageSourceCredentials>
  <fallbackPackageFolders>
    <clear />
  </fallbackPackageFolders>
</configuration>
EOF

# Copy solution and project files and restore
COPY LCU.API.Collections.CollectionsApi.sln .
COPY LCU.API.Collections.CollectionsApi/LCU.API.Collections.CollectionsApi.csproj ./LCU.API.Collections.CollectionsApi/
COPY LCU.API.Collections.CollectionsApiLib/LCU.API.Collections.CollectionsApiLib.csproj ./LCU.API.Collections.CollectionsApiLib/
COPY LCU.API.Collections.CollectionsApiTests/LCU.API.Collections.CollectionsApiTests.csproj ./LCU.API.Collections.CollectionsApiTests/
RUN dotnet restore -s "https://landmarkcu.pkgs.visualstudio.com/_packaging/LCU-Internal/nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json" ./LCU.API.Collections.CollectionsApi.sln

# Copy full source and build
COPY . .
RUN dotnet restore -s "https://landmarkcu.pkgs.visualstudio.com/_packaging/LCU-Internal/nuget/v3/index.json" -s "https://api.nuget.org/v3/index.json" ./LCU.API.Collections.CollectionsApi.sln
RUN dotnet build ./LCU.API.Collections.CollectionsApi/LCU.API.Collections.CollectionsApi.csproj -c Release --no-restore

# Test stage
FROM build AS test
WORKDIR /source
RUN dotnet test ./LCU.API.Collections.CollectionsApiTests/LCU.API.Collections.CollectionsApiTests.csproj -c Release --no-restore --logger "trx;LogFileName=test_results.trx"

# Publish stage
FROM build AS publish
WORKDIR /source
RUN dotnet publish ./LCU.API.Collections.CollectionsApi/LCU.API.Collections.CollectionsApi.csproj -c Release -o /app/publish --no-restore

# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS final
# Debian 10 (buster) is archived; repoint sources to archive.debian.org before installing packages.
RUN sed -i 's|deb.debian.org/debian|archive.debian.org/debian|g; s|security.debian.org/debian-security|archive.debian.org/debian-security|g; s|deb.debian.org/debian-security|archive.debian.org/debian-security|g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get install -y --no-install-recommends libicu-dev tzdata && \
    rm -rf /var/lib/apt/lists/*

ENV TZ=America/Chicago
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENV ASPNETCORE_ENVIRONMENT=Development

WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "LCU.API.Collections.CollectionsApi.dll"]
