# ------------------------------------------------------------------------------
# Stage 1: Builder
# This stage builds the application and its dependencies.
# ------------------------------------------------------------------------------
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS builder

WORKDIR /src

# Restore dependencies
COPY src/Dotnet.Samples.AspNetCore.WebApi/*.csproj  ./Dotnet.Samples.AspNetCore.WebApi/
RUN dotnet restore ./Dotnet.Samples.AspNetCore.WebApi

COPY src/Dotnet.Samples.AspNetCore.WebApi/          ./Dotnet.Samples.AspNetCore.WebApi/

WORKDIR /src/Dotnet.Samples.AspNetCore.WebApi

# Build solution and publish release
RUN dotnet publish -c Release -o /app/publish

# ------------------------------------------------------------------------------
# Stage 2: Runtime
# This stage creates the final, minimal image to run the application.
# ------------------------------------------------------------------------------
FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine AS runtime

WORKDIR /app

# Install curl for health check
RUN apk add --no-cache curl

# Metadata labels for the image. These are useful for registries and inspection.
LABEL org.opencontainers.image.title="🧪 Web API made with .NET 10 (LTS) and ASP.NET Core"
LABEL org.opencontainers.image.description="Proof of Concept for a Web API made with .NET 10 (LTS) and ASP.NET Core"
LABEL org.opencontainers.image.licenses="MIT"
LABEL org.opencontainers.image.source="https://github.com/nanotaboada/Dotnet.Samples.AspNetCore.WebApi"

# Set environment variables
ENV ASPNETCORE_URLS=http://+:9000
ENV ASPNETCORE_ENVIRONMENT=Production
# Alpine images default to invariant globalization mode. Explicitly opt in since
# this API uses ISO-8601 dates and ASCII data — ICU is not required.
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true

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

# Copy metadata docs for container registries (e.g.: GitHub Container Registry)
COPY --chmod=444        README.md                   ./

# https://rules.sonarsource.com/docker/RSPEC-6504/

# Copy entrypoint and healthcheck scripts
COPY --chmod=555        scripts/entrypoint.sh       ./entrypoint.sh
COPY --chmod=555        scripts/healthcheck.sh      ./healthcheck.sh

# Add non-root user and make volume mount point writable
RUN addgroup -S aspnetcore && \
    adduser -S -G aspnetcore aspnetcore && \
    mkdir -p /storage && \
    chown aspnetcore:aspnetcore /storage

USER aspnetcore

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
    CMD ["./healthcheck.sh"]

EXPOSE 9000

ENTRYPOINT ["./entrypoint.sh"]
CMD ["dotnet", "Dotnet.Samples.AspNetCore.WebApi.dll"]
