# Build stage / Giai đoạn build
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src

# EN: Copy project files for layer caching
# VI: Sao chép các file project để tận dụng layer caching
COPY ["src/AdsAnalyticsService.API/AdsAnalyticsService.API.csproj", "src/AdsAnalyticsService.API/"]
COPY ["src/AdsAnalyticsService.Domain/AdsAnalyticsService.Domain.csproj", "src/AdsAnalyticsService.Domain/"]
COPY ["src/AdsAnalyticsService.Infrastructure/AdsAnalyticsService.Infrastructure.csproj", "src/AdsAnalyticsService.Infrastructure/"]
COPY ["Directory.Build.props", "./"]

# EN: Restore dependencies
# VI: Khôi phục dependencies
RUN dotnet restore "src/AdsAnalyticsService.API/AdsAnalyticsService.API.csproj"

# EN: Copy all source code
# VI: Sao chép toàn bộ source code
COPY src/ ./src/

# EN: Build the application
# VI: Build ứng dụng
WORKDIR "/src/src/AdsAnalyticsService.API"
RUN dotnet build "AdsAnalyticsService.API.csproj" -c Release -o /app/build

# Publish stage / Giai đoạn publish
FROM build AS publish
RUN dotnet publish "AdsAnalyticsService.API.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Runtime stage / Giai đoạn runtime
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS final
WORKDIR /app

# EN: Create non-root user for security
# VI: Tạo user non-root cho bảo mật
RUN apt-get update && apt-get install -y curl --no-install-recommends && rm -rf /var/lib/apt/lists/* && \
    groupadd -g 1001 dotnetuser && \
    useradd -u 1001 -g dotnetuser -s /bin/sh dotnetuser

# EN: Copy published application
# VI: Sao chép ứng dụng đã publish
COPY --from=publish /app/publish .

# EN: Change ownership to non-root user
# VI: Thay đổi quyền sở hữu sang user non-root
RUN chown -R dotnetuser:dotnetuser /app

# EN: Switch to non-root user
# VI: Chuyển sang user non-root
USER dotnetuser

# EN: Expose port
# VI: Mở cổng
EXPOSE 8080

# EN: Set environment variables
# VI: Thiết lập biến môi trường
ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production

# EN: Health check
# VI: Kiểm tra health
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
    CMD curl -f http://localhost:8080/health/live || exit 1

# EN: Start the application
# VI: Khởi động ứng dụng
ENTRYPOINT ["dotnet", "AdsAnalyticsService.API.dll"]
# Build trigger 1775832654
# Build trigger v2 1775833389
# Build v3 1775834045
# Build v4 1775835811
# Build v5 1775838916
