FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src

# Copy project files and restore dependencies
COPY ["backend/AgentKit.Backend/AgentKit.Backend.csproj", "backend/AgentKit.Backend/"]
COPY ["backend/AgentKit.Frontend/AgentKit.Frontend.csproj", "backend/AgentKit.Frontend/"]
COPY ["backend/AgentKit.Shared/AgentKit.Shared.csproj", "backend/AgentKit.Shared/"]
RUN dotnet restore "backend/AgentKit.Backend/AgentKit.Backend.csproj"

# Build the project
COPY backend/ ./backend/
WORKDIR "/src/backend/AgentKit.Backend"
RUN dotnet build "AgentKit.Backend.csproj" -c Release -o /app/build

# Publish the project
RUN dotnet publish "AgentKit.Backend.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Final production stage
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS final
WORKDIR /app

# Ensure we have the assets folder at the correct relative path for Program.cs (which expects ../../assets)
COPY assets /assets

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

# Storage & DB Volumes
VOLUME [ "/app/Storage", "/app/agentkit.db" ]

# Set Kestrel to listen on 5050 and set environment vars
ENV ASPNETCORE_ENVIRONMENT=Production
ENV DOTNET_RUNNING_IN_CONTAINER=true

EXPOSE 5050
ENTRYPOINT ["dotnet", "AgentKit.Backend.dll"]
