# syntax=docker/dockerfile:1.7

# Build on the builder's native platform while publishing only the requested
# target architecture. This avoids copying every NuGet native runtime into the
# final image (SkiaSharp and SQLite otherwise add hundreds of megabytes).
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:10.0-alpine3.24 AS build
ARG TARGETARCH
WORKDIR /src

RUN apk add --no-cache binutils

COPY ClinicAPI.Core/*.vbproj ./ClinicAPI.Core/
RUN dotnet restore ClinicAPI.Core/ClinicAPI.Core.vbproj -a "$TARGETARCH"

COPY ClinicAPI.Core/ ./ClinicAPI.Core/
RUN dotnet publish ClinicAPI.Core/ClinicAPI.Core.vbproj \
    --configuration Release \
    --no-restore \
    --no-self-contained \
    --arch "$TARGETARCH" \
    --output /app/publish \
    -p:ContainerBuild=true \
    -p:UseAppHost=false \
    -p:DebugType=None \
    -p:DebugSymbols=false \
    && find /app/publish -maxdepth 1 -type f -name '*.a' -delete \
    && find /app/publish -maxdepth 1 -type f -name '*.so' \
        -exec strip --strip-unneeded '{}' +

# Composite Alpine is the smallest supported ASP.NET Core runtime variant that
# still provides the ICU and tzdata required for Arabic formatting and
# Africa/Cairo daylight-saving rules.
FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine3.24-composite-extra AS runtime
WORKDIR /app

# The API talks to the host daemon through /var/run/docker.sock. Install only
# the Docker CLI and Compose plugin plus Bash for the detached backup-volume
# activation helper; the Docker daemon, containerd and runc stay on the host.
RUN apk add --no-cache bash docker-cli docker-cli-compose jq

COPY --from=build /app/publish ./

# Reuse the two fonts already shipped with the Flutter web application. The
# renderer checks these stable Debian-compatible paths before font discovery,
# so a complete OS font pack is unnecessary.
RUN mkdir -p /usr/share/fonts/truetype/noto /app/wwwroot/downloads \
    && ln -s /app/wwwroot/app/assets/assets/fonts/NotoSans-Regular.ttf \
        /usr/share/fonts/truetype/noto/NotoSans-Regular.ttf \
    && ln -s /app/wwwroot/app/assets/assets/fonts/NotoSansArabic-Regular.ttf \
        /usr/share/fonts/truetype/noto/NotoSansArabic-Regular.ttf

ENV TZ=Etc/UTC
ENV ASPNETCORE_URLS=http://+:9427 \
    DOTNET_EnableDiagnostics=0 \
    DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

EXPOSE 9427
# BusyBox wget tries once; -T 5 makes it give up before Docker's 10 s
# timeout, so a slow answer on a busy host never leaves the probe behind.
HEALTHCHECK --interval=60s --timeout=10s --start-period=60s --retries=5 \
    CMD wget -q -T 5 --spider http://127.0.0.1:9427/api/health || exit 1
ENTRYPOINT ["dotnet", "ClinicAPI.Core.dll"]
