# Shadowed Realms API Dockerfile
# File Location: D:\shadowed-realms-mobile\ShadowedRealmsMobile\src\server\ShadowedRealms.API\Dockerfile
# Multi-stage build for production optimization

# ===============================
# BUILD STAGE
# ===============================
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

# Copy solution file (from root directory)
COPY ShadowedRealmsMobile.sln ./

# Copy project files for dependency restoration (all existing projects)
COPY src/server/ShadowedRealms.API/*.csproj ./src/server/ShadowedRealms.API/
COPY src/server/ShadowedRealms.Admin/*.csproj ./src/server/ShadowedRealms.Admin/
COPY src/server/ShadowedRealms.Core/*.csproj ./src/server/ShadowedRealms.Core/
COPY src/server/ShadowedRealms.Data/*.csproj ./src/server/ShadowedRealms.Data/
COPY src/server/ShadowedRealms.Shared/*.csproj ./src/server/ShadowedRealms.Shared/
COPY src/server/ShadowedRealms.SignalR/*.csproj ./src/server/ShadowedRealms.SignalR/

# Restore dependencies (this layer is cached if project files don't change)
RUN dotnet restore src/server/ShadowedRealms.API/ShadowedRealms.API.csproj

# Copy all source code
COPY src/server/ ./src/server/

# Build the application
RUN dotnet build src/server/ShadowedRealms.API/ShadowedRealms.API.csproj -c Release -o /app/build

# Publish the application
RUN dotnet publish src/server/ShadowedRealms.API/ShadowedRealms.API.csproj -c Release -o /app/publish /p:UseAppHost=false

# ===============================
# RUNTIME STAGE
# ===============================
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime

# Install curl for health checks
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*

# Create non-root user for security
RUN groupadd -r gameserver && useradd --no-log-init -r -g gameserver gameserver

WORKDIR /app

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

# Create log directory and set permissions
RUN mkdir -p /app/logs && chown -R gameserver:gameserver /app

# Switch to non-root user
USER gameserver

# ===============================
# CONFIGURATION
# ===============================

# Expose port 8080 (standard for containers)
EXPOSE 8080

# Environment variables
ENV ASPNETCORE_URLS=http://+:8080
ENV ASPNETCORE_ENVIRONMENT=Production
ENV DOTNET_PRINT_TELEMETRY_MESSAGE=false

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

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