# ColaFlow API Dockerfile
# Multi-stage build for .NET 9 application
# Optimized for modular monolith architecture with Docker layer caching

# ================================================================================================
# Stage 1: Build
# ================================================================================================
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src

# Copy solution file first
COPY ["ColaFlow.sln", "./"]

# Copy all project files for dependency restoration (leverages Docker cache)
# This layer will only rebuild if any .csproj file changes

# Core projects (old structure - still in use)
COPY ["src/ColaFlow.Domain/ColaFlow.Domain.csproj", "src/ColaFlow.Domain/"]
COPY ["src/ColaFlow.Application/ColaFlow.Application.csproj", "src/ColaFlow.Application/"]
COPY ["src/ColaFlow.Infrastructure/ColaFlow.Infrastructure.csproj", "src/ColaFlow.Infrastructure/"]
COPY ["src/ColaFlow.API/ColaFlow.API.csproj", "src/ColaFlow.API/"]

# Shared projects
COPY ["src/Shared/ColaFlow.Shared.Kernel/ColaFlow.Shared.Kernel.csproj", "src/Shared/ColaFlow.Shared.Kernel/"]

# Identity Module
COPY ["src/Modules/Identity/ColaFlow.Modules.Identity.Domain/ColaFlow.Modules.Identity.Domain.csproj", "src/Modules/Identity/ColaFlow.Modules.Identity.Domain/"]
COPY ["src/Modules/Identity/ColaFlow.Modules.Identity.Application/ColaFlow.Modules.Identity.Application.csproj", "src/Modules/Identity/ColaFlow.Modules.Identity.Application/"]
COPY ["src/Modules/Identity/ColaFlow.Modules.Identity.Infrastructure/ColaFlow.Modules.Identity.Infrastructure.csproj", "src/Modules/Identity/ColaFlow.Modules.Identity.Infrastructure/"]

# ProjectManagement Module
COPY ["src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Domain/ColaFlow.Modules.ProjectManagement.Domain.csproj", "src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Domain/"]
COPY ["src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Application/ColaFlow.Modules.ProjectManagement.Application.csproj", "src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Application/"]
COPY ["src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Infrastructure/ColaFlow.Modules.ProjectManagement.Infrastructure.csproj", "src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Infrastructure/"]
COPY ["src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Contracts/ColaFlow.Modules.ProjectManagement.Contracts.csproj", "src/Modules/ProjectManagement/ColaFlow.Modules.ProjectManagement.Contracts/"]

# IssueManagement Module
COPY ["src/Modules/IssueManagement/ColaFlow.Modules.IssueManagement.Domain/ColaFlow.Modules.IssueManagement.Domain.csproj", "src/Modules/IssueManagement/ColaFlow.Modules.IssueManagement.Domain/"]
COPY ["src/Modules/IssueManagement/ColaFlow.Modules.IssueManagement.Application/ColaFlow.Modules.IssueManagement.Application.csproj", "src/Modules/IssueManagement/ColaFlow.Modules.IssueManagement.Application/"]
COPY ["src/Modules/IssueManagement/ColaFlow.Modules.IssueManagement.Infrastructure/ColaFlow.Modules.IssueManagement.Infrastructure.csproj", "src/Modules/IssueManagement/ColaFlow.Modules.IssueManagement.Infrastructure/"]
COPY ["src/Modules/IssueManagement/ColaFlow.Modules.IssueManagement.Contracts/ColaFlow.Modules.IssueManagement.Contracts.csproj", "src/Modules/IssueManagement/ColaFlow.Modules.IssueManagement.Contracts/"]

# Restore NuGet packages
# This layer is cached unless .csproj files change
RUN dotnet restore "src/ColaFlow.API/ColaFlow.API.csproj"

# Copy the rest of the source code
# This layer rebuilds whenever source code changes
COPY . .

# Build the application
WORKDIR "/src/src/ColaFlow.API"
RUN dotnet build "ColaFlow.API.csproj" -c Release -o /app/build --no-restore

# ================================================================================================
# Stage 2: Publish
# ================================================================================================
FROM build AS publish
RUN dotnet publish "ColaFlow.API.csproj" -c Release -o /app/publish --no-restore

# ================================================================================================
# Stage 3: Runtime (Alpine for smaller image size)
# ================================================================================================
FROM mcr.microsoft.com/dotnet/aspnet:9.0-alpine AS runtime
WORKDIR /app

# Install curl for health checks (alpine uses apk)
RUN apk add --no-cache curl

# Create a non-root user for security
RUN addgroup -g 1000 appgroup && \
    adduser -D -u 1000 -G appgroup appuser

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

# Set ownership of application files
RUN chown -R appuser:appgroup /app

# Configure ASP.NET Core
ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Development

# Expose HTTP port (HTTPS will be handled by reverse proxy in production)
EXPOSE 8080

# Health check endpoint
HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=40s \
  CMD curl -f http://localhost:8080/health || exit 1

# Run as non-root user for security
USER appuser

# Entry point
ENTRYPOINT ["dotnet", "ColaFlow.API.dll"]
