FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8070
EXPOSE 8071


FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["ConstructorAppApi/ConstructorAppApi.csproj", "ConstructorAppApi/"]
COPY ["ConstructorApp.BusinessLayer/ConstructorApp.BusinessLayer.csproj", "ConstructorApp.BusinessLayer/"]
COPY ["ConstructorApp.DataAccessLayer/ConstructorApp.DataAccessLayer.csproj", "ConstructorApp.DataAccessLayer/"]
COPY ["ConstructorApp.EntityLayer/ConstructorApp.EntityLayer.csproj", "ConstructorApp.EntityLayer/"]
COPY ["ConstructorApp.DtoLayer/ConstructorApp.DtoLayer.csproj", "ConstructorApp.DtoLayer/"]
RUN dotnet restore "./ConstructorAppApi/ConstructorAppApi.csproj"
COPY . .
WORKDIR "/src/ConstructorAppApi"
RUN dotnet build "./ConstructorAppApi.csproj" -c $BUILD_CONFIGURATION -o /app/build


FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./ConstructorAppApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

RUN apt-get update \
    && apt-get install -y openssl

# Create directories for the certificate
RUN mkdir -p /app/publish/https

# Generate a self-signed certificate
RUN openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /app/publish/https/aspnetapp.key -out /app/publish/https/aspnetapp.crt -subj "/CN=localhost"
RUN openssl pkcs12 -export -out /app/publish/https/aspnetapp.pfx -inkey /app/publish/https/aspnetapp.key -in /app/publish/https/aspnetapp.crt -passout pass:itRkTNSFBEFPqCGdYEuIloyhx


# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
USER root
WORKDIR /app
COPY --from=publish /app/publish .
RUN chmod -R 777 /app/https
ENTRYPOINT ["dotnet", "ConstructorAppApi.dll"]