# Stage 1: Restore (cached when .csproj/.slnx files don't change)
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS restore
WORKDIR /src

# Copy all .csproj files first (rarely change) for restore layer caching
COPY src/ScadaLink.Commons/ScadaLink.Commons.csproj src/ScadaLink.Commons/
COPY src/ScadaLink.Host/ScadaLink.Host.csproj src/ScadaLink.Host/
COPY src/ScadaLink.TemplateEngine/ScadaLink.TemplateEngine.csproj src/ScadaLink.TemplateEngine/
COPY src/ScadaLink.DeploymentManager/ScadaLink.DeploymentManager.csproj src/ScadaLink.DeploymentManager/
COPY src/ScadaLink.SiteRuntime/ScadaLink.SiteRuntime.csproj src/ScadaLink.SiteRuntime/
COPY src/ScadaLink.DataConnectionLayer/ScadaLink.DataConnectionLayer.csproj src/ScadaLink.DataConnectionLayer/
COPY src/ScadaLink.Communication/ScadaLink.Communication.csproj src/ScadaLink.Communication/
COPY src/ScadaLink.StoreAndForward/ScadaLink.StoreAndForward.csproj src/ScadaLink.StoreAndForward/
COPY src/ScadaLink.ExternalSystemGateway/ScadaLink.ExternalSystemGateway.csproj src/ScadaLink.ExternalSystemGateway/
COPY src/ScadaLink.NotificationService/ScadaLink.NotificationService.csproj src/ScadaLink.NotificationService/
COPY src/ScadaLink.CentralUI/ScadaLink.CentralUI.csproj src/ScadaLink.CentralUI/
COPY src/ScadaLink.Security/ScadaLink.Security.csproj src/ScadaLink.Security/
COPY src/ScadaLink.HealthMonitoring/ScadaLink.HealthMonitoring.csproj src/ScadaLink.HealthMonitoring/
COPY src/ScadaLink.SiteEventLogging/ScadaLink.SiteEventLogging.csproj src/ScadaLink.SiteEventLogging/
COPY src/ScadaLink.ClusterInfrastructure/ScadaLink.ClusterInfrastructure.csproj src/ScadaLink.ClusterInfrastructure/
COPY src/ScadaLink.InboundAPI/ScadaLink.InboundAPI.csproj src/ScadaLink.InboundAPI/
COPY src/ScadaLink.ConfigurationDatabase/ScadaLink.ConfigurationDatabase.csproj src/ScadaLink.ConfigurationDatabase/
COPY src/ScadaLink.ManagementService/ScadaLink.ManagementService.csproj src/ScadaLink.ManagementService/
COPY lmxproxy/src/ZB.MOM.WW.LmxProxy.Client/ZB.MOM.WW.LmxProxy.Client.csproj lmxproxy/src/ZB.MOM.WW.LmxProxy.Client/

# Restore NuGet packages via Host project (follows ProjectReferences to all dependencies)
# This layer is cached until any .csproj changes — source-only changes skip restore entirely
RUN dotnet restore src/ScadaLink.Host/ScadaLink.Host.csproj

# Stage 2: Build + Publish
FROM restore AS build
COPY src/ src/
COPY lmxproxy/src/ZB.MOM.WW.LmxProxy.Client/ lmxproxy/src/ZB.MOM.WW.LmxProxy.Client/
RUN dotnet publish src/ScadaLink.Host/ScadaLink.Host.csproj \
    -c Release -o /app/publish

# Stage 3: Runtime (minimal image, no SDK)
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS runtime
WORKDIR /app

# Create data directory for SQLite databases (site nodes)
RUN mkdir -p /app/data /app/logs

COPY --from=build /app/publish .

EXPOSE 5000 8081 8082

ENTRYPOINT ["dotnet", "ScadaLink.Host.dll"]
