﻿# ---- 1. Base Image for Runtime ----
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 5298

# ---- 2. Build Image ----
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

# Copy only the .csproj files first to optimize caching
COPY src/FileService/FileService.csproj FileService/
COPY src/MeteorCloud.Shared/MeteorCloud.Shared.csproj MeteorCloud.Shared/
COPY src/MeteorCloud.Messaging/MeteorCloud.Messaging.csproj MeteorCloud.Messaging/
COPY src/MeteorCloud.Communication/MeteorCloud.Communication.csproj MeteorCloud.Communication/

# Restore dependencies for all projects
RUN dotnet restore FileService/FileService.csproj

# Copy everything else inside both projects
COPY src/FileService/. FileService/
COPY src/MeteorCloud.Shared/. MeteorCloud.Shared/
COPY src/MeteorCloud.Messaging/. MeteorCloud.Messaging/
COPY src/MeteorCloud.Communication/. MeteorCloud.Communication/

# Set working directory inside FileService
WORKDIR /src/FileService

# Create a clean output directory for the build
RUN mkdir -p /app/build && chmod -R 777 /app/build

# Build the application (ensure the correct output path)
RUN dotnet build "FileService.csproj" -c Release -o /app/build --no-restore

# ---- 3. Publish the App (Do Not Skip Build) ----
FROM build AS publish
RUN dotnet publish "FileService.csproj" -c Release -o /app/publish --no-restore

# ---- 4. Final Runtime Image ----
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

# Run the application
ENTRYPOINT ["dotnet", "FileService.dll"]