#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

# Use the official Microsoft ASP.NET Core image to build the backend
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-backend
WORKDIR "/src/backend"
COPY Back/ .
RUN dotnet restore "skydiveLogs-api/skydiveLogs-api.csproj"
RUN dotnet publish "skydiveLogs-api/skydiveLogs-api.csproj" -c Release -o /app/publish

# Use the official node image to build the Angular app
FROM node:20-alpine AS build-frontend
WORKDIR /app
COPY ["Front/skydivelogs-app/package.json", "Front/skydivelogs-app/package-lock.json*", "./"]
RUN npm install
COPY --exclude=Front/skydivelogs-app/node_modules/* Front/skydivelogs-app/ .
RUN npm run build

# Use a .NET runtime image to serve both the backend and frontend
FROM mcr.microsoft.com/dotnet/aspnet:8.0.16-bookworm-slim-amd64 AS final 
WORKDIR /app

# Install nginx
RUN apt-get update && apt-get install -y nginx curl && rm -rf /var/lib/apt/lists/*

# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/sites-available/default
RUN rm -rf /usr/share/nginx/html/*

# Copy frontend dist folder to nginx html directory
COPY --from=build-frontend /app/dist /usr/share/nginx/html

# Copy backend from the correct build stage
COPY --from=build-backend /app/publish /app

# Expose port 80 for the application
EXPOSE 80

ENV ASPNETCORE_URLS http://+:5001

VOLUME /app/Data
VOLUME /usr/share/nginx/html/config

# Start nginx and the .NET Core app
CMD ["sh", "-c", "dotnet /app/skydiveLogs-api.dll & nginx -g 'daemon off;'"]