#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.

# node version changelog: https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V14.md
# use local node image
ARG NODE_IMAGE=node.14.18.1:latest

FROM aspnet.8.0:latest AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443


FROM sdk.8.0:latest AS build
WORKDIR /src
COPY ["Aeroflot.Flights.Web/Aeroflot.Flights.Web.csproj", "Aeroflot.Flights.Web/"]
COPY ["Aeroflot.Flights.WebApi/Aeroflot.Flights.WebApi.csproj", "Aeroflot.Flights.WebApi/"]
COPY ["Aeroflot.Flights.BL/Aeroflot.Flights.BL.csproj", "Aeroflot.Flights.BL/"]
COPY ["Aeroflot.Common.ApiWrappers/Aeroflot.Common.ApiWrappers.csproj", "Aeroflot.Common.ApiWrappers/"]
COPY ["Aeroflot.Flights.Interfaces/Aeroflot.Flights.Interfaces.csproj", "Aeroflot.Flights.Interfaces/"]
COPY ["Aeroflot.Flights.Entities/Aeroflot.Flights.Entities.csproj", "Aeroflot.Flights.Entities/"]
COPY ["Aeroflot.Flights.DataSpace/Aeroflot.Flights.DataSpace.csproj", "Aeroflot.Flights.DataSpace/"]

ARG NUGET
ARG NUGET_USER
ARG NUGET_PASSWORD
COPY ["docker.nuget.config", "nuget.config"]

RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages dotnet restore "Aeroflot.Flights.Web/Aeroflot.Flights.Web.csproj"
COPY . .
WORKDIR "/src/Aeroflot.Flights.Web"
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages dotnet build --no-restore "Aeroflot.Flights.Web.csproj" -c Release -o /app/build

FROM build AS publish
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages dotnet publish --no-restore "Aeroflot.Flights.Web.csproj" -c Release -o /app/publish /p:UseAppHost=false

## Build Angular files
FROM ${NODE_IMAGE} as node-build
WORKDIR /src
COPY Aeroflot.Flights.Web/ClientApp .

ARG NPM_PROXY
RUN npm config set registry $NPM_PROXY
RUN npm ci
RUN npm rebuild node-sass
ARG ANGULAR_ENVIRONMENT
RUN npm run build -- --configuration=$ANGULAR_ENVIRONMENT
## ####

FROM base AS final
# add NTLM support for EWS mail sender
RUN apt-get update && apt-get install -y --no-install-recommends gss-ntlmssp && rm -rf /var/lib/apt/lists/*

WORKDIR /app
#copy .net published files
COPY --from=publish /app/publish .
#copy angular compiled files
COPY --from=node-build /src/dist ./ClientApp/dist
ENTRYPOINT ["dotnet", "Aeroflot.Flights.Web.dll"]