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

# Cache de NuGet packages - copiamos solo archivos de proyecto primero
COPY src/backend/Domain/Domain.csproj Domain/
COPY src/backend/Application/Application.csproj Application/
COPY src/backend/Adapters/Driving/Api/Adapters.Driving.Api.csproj Adapters/Driving/Api/
COPY src/backend/Adapters/Driven/Persistence/Adapters.Driven.Persistence.csproj Adapters/Driven/Persistence/
COPY src/backend/Host/Host.csproj Host/

# Restore
RUN dotnet restore Host/Host.csproj

# Copiar código fuente
COPY src/backend/Domain/ Domain/
COPY src/backend/Application/ Application/
COPY src/backend/Adapters/ Adapters/
COPY src/backend/Host/ Host/

# Build y publish
RUN dotnet publish Host/Host.csproj \
    -c Release \
    -o /app

# Runtime image
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS runtime
WORKDIR /app

# Instalar curl para healthcheck
RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*

# Copiar binarios
COPY --from=build /app .

# Configuración
ENV ASPNETCORE_URLS=http://+:5000 \
    DOTNET_RUNNING_IN_CONTAINER=true

EXPOSE 5000

HEALTHCHECK --interval=15s --timeout=3s --start-period=10s --retries=3 \
  CMD curl -f http://localhost:5000/health || exit 1

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