FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM node:22-bookworm-slim AS node

FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG TARGETARCH
ENV DOTNET_CLI_TELEMETRY_OPTOUT=1 \
    NUGET_XMLDOC_MODE=skip
WORKDIR /src

# Bring over Node & NPM runtimes for administrative UI asset packaging steps
COPY --from=node /usr/local/bin/node /usr/local/bin/node
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
    && npm install -g npm@latest \
    && node --version && npm --version

# 1. Copy the master solution file and project structures for build cache retention
COPY LiteCharmsSecurity.sln ./
COPY src/*/*.csproj ./
COPY src/ ./src/
# Clean up duplicate files dropped into the root context by the wildcard copy
RUN rm -f *.csproj

# 2. Restore the entire solution cleanly via public NuGet paths
RUN dotnet restore "LiteCharmsSecurity.sln"

# 3. Pull in the remaining implementation files
COPY . .

# 4. Navigate into your specific Admin UI directory context for assembly compilation
WORKDIR "/src/src/LiteCharmsSecurity.Admin"
RUN dotnet build "LiteCharmsSecurity.Admin.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "LiteCharmsSecurity.Admin.csproj" -c Release --no-restore -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV ASPNETCORE_FORWARDEDHEADERS_ENABLED=true
ENTRYPOINT ["dotnet", "LiteCharmsSecurity.Admin.dll"]