# ═══════════════════════════════════════════════════════════════════════════════
# EN: Eggymon Kitchen Landing Page - Multi-stage Docker Build
# VI: Eggymon Kitchen Landing Page - Build Docker đa giai đoạn
# ═══════════════════════════════════════════════════════════════════════════════

# ─── Stage 1: Build ──────────────────────────────────────────────────────────
FROM mcr.microsoft.com/dotnet/sdk:10.0-alpine AS build
WORKDIR /src

# EN: Copy project files first for better layer caching
# VI: Copy file dự án trước để cache layer tốt hơn
COPY src/EggymonLandingPage.Shared/EggymonLandingPage.Shared.csproj src/EggymonLandingPage.Shared/
COPY src/EggymonLandingPage.Client/EggymonLandingPage.Client.csproj src/EggymonLandingPage.Client/
COPY src/EggymonLandingPage.Server/EggymonLandingPage.Server.csproj src/EggymonLandingPage.Server/

# EN: Restore dependencies
# VI: Restore các gói phụ thuộc
RUN dotnet restore src/EggymonLandingPage.Server/EggymonLandingPage.Server.csproj

# EN: Copy all source code
# VI: Copy toàn bộ mã nguồn
COPY . .

# EN: Build and Publish
# VI: Build và Publish
RUN dotnet publish src/EggymonLandingPage.Server/EggymonLandingPage.Server.csproj \
    -c Release \
    -o /app/publish \
    --no-restore

# ─── Stage 2: Runtime ────────────────────────────────────────────────────────
FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine AS runtime
WORKDIR /app

# EN: Create non-root user for security
# VI: Tạo user non-root cho bảo mật
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# EN: Copy published output
# VI: Copy kết quả publish
COPY --from=build /app/publish .

# EN: Set ownership to non-root user
# VI: Đặt quyền sở hữu cho user non-root
RUN chown -R appuser:appgroup /app

USER appuser

# EN: Expose port 8080 (default non-root port)
# VI: Expose cổng 8080 (cổng mặc định non-root)
EXPOSE 8080
ENV ASPNETCORE_URLS=http://+:8080

# EN: Health check
# VI: Kiểm tra sức khỏe
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1

ENTRYPOINT ["dotnet", "EggymonLandingPage.Server.dll"]
