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

# ---- 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/LinkService/LinkService.csproj LinkService/
COPY src/MeteorCloud.Caching/MeteorCloud.Caching.csproj MeteorCloud.Caching/
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 LinkService/LinkService.csproj

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

# Set working directory inside UserService
WORKDIR /src/LinkService

# 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 "LinkService.csproj" -c Release -o /app/build --no-restore

# ---- 3. Publish the App (Do Not Skip Build) ----
FROM build AS publish
RUN dotnet publish "LinkService.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", "LinkService.dll"]