# ═══════════════════════════════════════════════════════════════════════════════
# WebClientTpos Dockerfile
# EN: Multi-stage build for Blazor WebAssembly Hosted
# VI: Multi-stage build cho Blazor WebAssembly Hosted
# ═══════════════════════════════════════════════════════════════════════════════

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

# EN: Copy solution and project files for layer caching
# VI: Copy solution và project files để cache layers
COPY WebClientTpos.slnx ./
COPY src/WebClientTpos.Shared/WebClientTpos.Shared.csproj ./src/WebClientTpos.Shared/
COPY src/WebClientTpos.Client/WebClientTpos.Client.csproj ./src/WebClientTpos.Client/
COPY src/WebClientTpos.Server/WebClientTpos.Server.csproj ./src/WebClientTpos.Server/

# EN: Restore dependencies
# VI: Restore dependencies
RUN dotnet restore

# EN: Copy source code
# VI: Copy source code
COPY . .

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

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

# EN: Install ICU libs for globalization support (Alpine doesn't include them)
# VI: Cài đặt ICU libs cho hỗ trợ globalization (Alpine không bao gồm sẵn)
RUN apk add --no-cache icu-libs
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

# EN: Create non-root user for security
# VI: Tạo user không phải root để bảo mật
RUN adduser -D -u 1000 appuser && chown -R appuser /app
USER appuser

# EN: Copy published output
# VI: Copy output đã publish
COPY --from=build /app/publish .

# EN: Expose port
# VI: Expose port
EXPOSE 8080

# EN: Health check
# VI: Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1

# 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: Run the application
# VI: Chạy ứng dụng
ENTRYPOINT ["dotnet", "WebClientTpos.Server.dll"]
