# 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/ZB.MOM.WW.ScadaBridge.Commons/ZB.MOM.WW.ScadaBridge.Commons.csproj src/ZB.MOM.WW.ScadaBridge.Commons/
COPY src/ZB.MOM.WW.ScadaBridge.Host/ZB.MOM.WW.ScadaBridge.Host.csproj src/ZB.MOM.WW.ScadaBridge.Host/
COPY src/ZB.MOM.WW.ScadaBridge.TemplateEngine/ZB.MOM.WW.ScadaBridge.TemplateEngine.csproj src/ZB.MOM.WW.ScadaBridge.TemplateEngine/
COPY src/ZB.MOM.WW.ScadaBridge.DeploymentManager/ZB.MOM.WW.ScadaBridge.DeploymentManager.csproj src/ZB.MOM.WW.ScadaBridge.DeploymentManager/
COPY src/ZB.MOM.WW.ScadaBridge.SiteRuntime/ZB.MOM.WW.ScadaBridge.SiteRuntime.csproj src/ZB.MOM.WW.ScadaBridge.SiteRuntime/
COPY src/ZB.MOM.WW.ScadaBridge.DataConnectionLayer/ZB.MOM.WW.ScadaBridge.DataConnectionLayer.csproj src/ZB.MOM.WW.ScadaBridge.DataConnectionLayer/
COPY src/ZB.MOM.WW.ScadaBridge.Communication/ZB.MOM.WW.ScadaBridge.Communication.csproj src/ZB.MOM.WW.ScadaBridge.Communication/
COPY src/ZB.MOM.WW.ScadaBridge.StoreAndForward/ZB.MOM.WW.ScadaBridge.StoreAndForward.csproj src/ZB.MOM.WW.ScadaBridge.StoreAndForward/
COPY src/ZB.MOM.WW.ScadaBridge.ExternalSystemGateway/ZB.MOM.WW.ScadaBridge.ExternalSystemGateway.csproj src/ZB.MOM.WW.ScadaBridge.ExternalSystemGateway/
COPY src/ZB.MOM.WW.ScadaBridge.NotificationService/ZB.MOM.WW.ScadaBridge.NotificationService.csproj src/ZB.MOM.WW.ScadaBridge.NotificationService/
COPY src/ZB.MOM.WW.ScadaBridge.CentralUI/ZB.MOM.WW.ScadaBridge.CentralUI.csproj src/ZB.MOM.WW.ScadaBridge.CentralUI/
COPY src/ZB.MOM.WW.ScadaBridge.Security/ZB.MOM.WW.ScadaBridge.Security.csproj src/ZB.MOM.WW.ScadaBridge.Security/
COPY src/ZB.MOM.WW.ScadaBridge.HealthMonitoring/ZB.MOM.WW.ScadaBridge.HealthMonitoring.csproj src/ZB.MOM.WW.ScadaBridge.HealthMonitoring/
COPY src/ZB.MOM.WW.ScadaBridge.SiteEventLogging/ZB.MOM.WW.ScadaBridge.SiteEventLogging.csproj src/ZB.MOM.WW.ScadaBridge.SiteEventLogging/
COPY src/ZB.MOM.WW.ScadaBridge.ClusterInfrastructure/ZB.MOM.WW.ScadaBridge.ClusterInfrastructure.csproj src/ZB.MOM.WW.ScadaBridge.ClusterInfrastructure/
COPY src/ZB.MOM.WW.ScadaBridge.InboundAPI/ZB.MOM.WW.ScadaBridge.InboundAPI.csproj src/ZB.MOM.WW.ScadaBridge.InboundAPI/
COPY src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.csproj src/ZB.MOM.WW.ScadaBridge.ConfigurationDatabase/
COPY src/ZB.MOM.WW.ScadaBridge.ManagementService/ZB.MOM.WW.ScadaBridge.ManagementService.csproj src/ZB.MOM.WW.ScadaBridge.ManagementService/

# Central Package Management: every .csproj declares versionless PackageReferences
# and the versions live in Directory.Packages.props. It must be present (above the
# projects) for `dotnet restore` to resolve versions — without it restore fails NU1015.
COPY Directory.Packages.props ./

# nuget.config declares the Gitea feed (and package-source mapping) that serves the
# ZB.MOM.WW.MxGateway.* packages used by the Data Connection Layer.
COPY nuget.config ./

# Optional credentials for the private Gitea feed, supplied at build time via
# --build-arg (see docker/build.sh). Left blank for an anonymous feed. NuGet reads
# per-source credentials from the NuGetPackageSourceCredentials_<sourceName> env var.
ARG NUGET_GITEA_USER=
ARG NUGET_GITEA_PASS=
ENV NuGetPackageSourceCredentials_dohertj2-gitea="Username=${NUGET_GITEA_USER};Password=${NUGET_GITEA_PASS}"

# 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/ZB.MOM.WW.ScadaBridge.Host/ZB.MOM.WW.ScadaBridge.Host.csproj

# Stage 2: Build + Publish
FROM restore AS build
COPY src/ src/
RUN dotnet publish src/ZB.MOM.WW.ScadaBridge.Host/ZB.MOM.WW.ScadaBridge.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", "ZB.MOM.WW.ScadaBridge.Host.dll"]
