# Dockerfile per TradingBot - Multi-stage build ottimizzato
# Stage 1: Build
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src

# Copy csproj e restore dipendenze (layer caching)
COPY ["TradingBot.csproj", "./"]
RUN dotnet restore "TradingBot.csproj"

# Copy tutto il codice sorgente
COPY . .

# Build in Release mode
RUN dotnet build "TradingBot.csproj" -c Release -o /app/build

# Stage 2: Publish
FROM build AS publish
RUN dotnet publish "TradingBot.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Stage 3: Runtime
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final
WORKDIR /app

# Installa wget per health check (curl non disponibile nell'immagine base)
RUN apt-get update && apt-get install -y wget && rm -rf /var/lib/apt/lists/*

# Crea utente non-root per sicurezza
# Usa UID 1001 invece di 1000 (1000 spesso già in uso nell'immagine base)
RUN groupadd -r -g 1001 tradingbot && \
    useradd -r -u 1001 -g tradingbot -m -s /bin/bash tradingbot && \
    chown -R tradingbot:tradingbot /app

# Esponi porta
EXPOSE 8080

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

# Crea directory per persistenza dati
RUN mkdir -p /app/data && \
    chown -R tradingbot:tradingbot /app/data

# Volume per dati persistenti
VOLUME ["/app/data"]

# Switch a utente non-root
USER tradingbot

# Environment variables
ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production
ENV DOTNET_RUNNING_IN_CONTAINER=true

# Health check con wget invece di curl
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1

# Entry point
ENTRYPOINT ["dotnet", "TradingBot.dll"]
