# ------------------------
# Runtime Base Image
# ------------------------
FROM mcr.microsoft.com/dotnet/aspnet:8.0.15@sha256:6159cf66274cf52730d7a0c7bb05cf0af94b79370176886ac58286ab6cbb7faf AS base
ENV TZ="Asia/Kolkata"
WORKDIR /app
EXPOSE 80
EXPOSE 443

# ------------------------
# SDK Build Image
# ------------------------
FROM mcr.microsoft.com/dotnet/sdk:8.0.203@sha256:249a78aa4ce22ab872d0dff0490a6389e7bc087d2c080c4ffc7569b49cf0e23b AS build
WORKDIR /src
ARG ENVIRONMENT=Release

# Copy csproj and restore
COPY ["User.Insight.Tracker/User.Insight.Tracker.csproj", "User.Insight.Tracker/"]
RUN dotnet restore "User.Insight.Tracker/User.Insight.Tracker.csproj"

# Copy everything and build
COPY . .
WORKDIR "/src/User.Insight.Tracker"
RUN dotnet build "User.Insight.Tracker.csproj" -c $ENVIRONMENT -o /app/build

# ------------------------
# Publish App
# ------------------------
FROM build AS publish
RUN dotnet publish "User.Insight.Tracker.csproj" -c $ENVIRONMENT -o /app/publish

# ------------------------
# Final Runtime Image
# ------------------------
FROM base AS final
WORKDIR /app

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

# Create non-root user and switch to it
RUN addgroup --system computracegrp && adduser --system --ingroup computracegrp computrace
USER computrace

# Set environment variable for ASP.NET Core to listen on port 80
ENV ASPNETCORE_URLS=http://+:80

# Run the app
ENTRYPOINT ["dotnet", "User.Insight.Tracker.dll"]
