# ------------------------------------- BUILD -------------------------------------
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["AI-Health-Coach.csproj", "./"]
RUN dotnet restore "AI-Health-Coach.csproj"
COPY . .
WORKDIR /src/.
RUN dotnet build "AI-Health-Coach.csproj" -c Release -o /app/build

# ------------------------------------- PUBLISH -------------------------------------

# Published the application to "app/publish"
FROM build AS publish
RUN dotnet publish "AI-Health-Coach.csproj" -c Release -o /app/publish

# --------------------------------------- RUN ---------------------------------------

FROM mcr.microsoft.com/dotnet/aspnet:7.0

# Local location of certificates
ARG L_CA_CERT=./certs/cert.pem
ARG L_CA_KEY=./certs/cert.key

# Container location of certificates
ARG C_CA_CERT=/app/certs/cert.pem
ARG C_CA_KEY=/app/certs/cert.key

# Set Kestrel webserver environment variables
ENV Kestrel:Certificates:Default:Path=${CA_CERT}
ENV Kestrel:Certificates:Default:KeyPath=${CA_KEY}

# Copy from local to docker container
COPY ${L_CA_CERT} ${C_CA_CERT}
COPY ${L_CA_KEY} ${C_CA_KEY} 

WORKDIR /app

# SSL gets terminated at the load balancer and port 80 is used internally
EXPOSE 80 

# Copied data from publish stage
COPY --from=publish /app/publish .

# Defines the entrypoint for the application
ENTRYPOINT ["dotnet", "AI-Health-Coach.dll"]