# Backend API - Multi-stage optimized build
FROM mcr.microsoft.com/dotnet/sdk:10.0-alpine AS build
WORKDIR /src

# Copy csproj files first for layer caching
COPY src/backend/Domain/*.csproj Domain/
COPY src/backend/Application/*.csproj Application/
COPY src/backend/Adapters/Driven/Persistence/*.csproj Adapters/Driven/Persistence/
COPY src/backend/Adapters/Driving/Api/*.csproj Adapters/Driving/Api/
COPY src/backend/Host/*.csproj Host/

# Restore dependencies for Alpine/musl
RUN dotnet restore Host/Host.csproj -r linux-musl-x64

# Copy source and build
COPY src/backend/ .
RUN dotnet publish Host/Host.csproj -c Release -o /app -r linux-musl-x64 --self-contained false

# Runtime image
FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine AS runtime
# Install ICU for SQL Server globalization support
RUN apk add --no-cache icu-libs
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
WORKDIR /app
COPY --from=build /app .

ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production

EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s CMD wget -q --spider http://localhost:8080/health || exit 1

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