FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
RUN apt-get update && apt-get install -y build-essential clang zlib1g-dev && rm -rf /var/lib/apt/lists/*
WORKDIR /src

COPY ["JMonServer/JMonServer.csproj", "JMonServer/"]
COPY ["JMonAgent/JMonAgent.csproj", "JMonAgent/"]
COPY ["Protos/", "Protos/"]

RUN dotnet restore "JMonServer/JMonServer.csproj"

COPY . .
RUN dotnet build "JMonServer/JMonServer.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "JMonServer/JMonServer.csproj" -c Release -o /app/publish

FROM mcr.microsoft.com/dotnet/runtime-deps:10.0-noble
RUN apt-get update && apt-get install -y openssl && rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=publish /app/publish .

# Generate self-signed PFX certificate for HTTPS (no password)
RUN mkdir -p /root/.aspnet/https && \
    openssl req -x509 -newkey rsa:4096 -keyout /tmp/key.pem -out /tmp/cert.pem \
    -days 365 -nodes -subj "/CN=*" && \
    openssl pkcs12 -export -out /root/.aspnet/https/aspnetapp.pfx -inkey /tmp/key.pem -in /tmp/cert.pem -passout pass: && \
    rm /tmp/key.pem /tmp/cert.pem

EXPOSE 5001
ENV ASPNETCORE_URLS=https://0.0.0.0:5001
ENV ASPNETCORE_Kestrel__Certificates__Default__Path=/root/.aspnet/https/aspnetapp.pfx
ENV ASPNETCORE_Kestrel__Certificates__Default__Password=
ENV ASPNETCORE_ENVIRONMENT=Development

ENTRYPOINT ["./JMonServer"]
