# Dockerfile self-contained para máxima performance
FROM --platform=linux/arm64 mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

FROM --platform=linux/arm64 mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src

# Copiar apenas arquivos de projeto primeiro (melhor cache)
COPY ["OnlyOneAccessTemplate/OnlyOneAccessTemplate.csproj", "OnlyOneAccessTemplate/"]

# Restore com configurações otimizadas para ARM64
RUN dotnet restore "./OnlyOneAccessTemplate/OnlyOneAccessTemplate.csproj" \
    --runtime linux-arm64 \
    --no-cache

# Copiar código fonte
COPY . .
WORKDIR "/src/OnlyOneAccessTemplate"

# Build otimizado
RUN dotnet build "./OnlyOneAccessTemplate.csproj" \
    -c $BUILD_CONFIGURATION \
    -o /app/build \
    --runtime linux-arm64 \
    --no-restore

FROM build AS publish
ARG BUILD_CONFIGURATION=Release

# Publish self-contained para eliminar cold start
RUN dotnet publish "./OnlyOneAccessTemplate.csproj" \
    -c $BUILD_CONFIGURATION \
    -o /app/publish \
    --runtime linux-arm64 \
    --no-restore \
    --self-contained true \
    /p:PublishTrimmed=true \
    /p:PublishSingleFile=false

FROM mcr.microsoft.com/dotnet/runtime-deps:8.0 AS final
WORKDIR /app
COPY --from=publish /app/publish .

# Variáveis de ambiente otimizadas para produção
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true
ENV DOTNET_USE_POLLING_FILE_WATCHER=true
ENV ASPNETCORE_ENVIRONMENT=Production
ENV DOTNET_EnableDiagnostics=0

# Healthcheck simples
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:8080/health || exit 1

ENTRYPOINT ["./OnlyOneAccessTemplate"]