# Use the official .NET SDK image to build the application
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build

# Set the working directory
WORKDIR /src

# Copy the solution file and restore dependencies
COPY ["Homework.slnx", "."]
COPY ["Homework/Homework.csproj", "Homework/"]
COPY ["Logger/Logger.csproj", "Logger/"]

# Restore dependencies
RUN dotnet restore

# Copy the entire source code
COPY . .

# Build the application
RUN dotnet build "Homework/Homework.csproj" -c Release -o /app/build

# Publish the application
FROM build AS publish
RUN dotnet publish "Homework/Homework.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Use the official ASP.NET Core runtime image for the final stage
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final

# Set the working directory
WORKDIR /app

# Create a non-root user
RUN adduser --disabled-password --gecos '' appuser && chown -R appuser:appuser /app
USER appuser

# Copy the published application
COPY --from=publish /app/publish .

# Expose the port the app runs on
EXPOSE 8080

# Set environment variables
ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production

# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD curl -f http://localhost:8080/health || exit 1

# Run the application
ENTRYPOINT ["dotnet", "Homework.dll"]