# Use the .NET 8.0 SDK as the base image for the build stage
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source

# Copy the .csproj files for each project
COPY src/Ellie.Marmalade/*.csproj src/Ellie.Marmalade/
COPY src/EllieBot/*.csproj src/EllieBot/
COPY src/EllieBot.Coordinator/*.csproj src/EllieBot.Coordinator/
COPY src/EllieBot.Generators/*.csproj src/EllieBot.Generators/
COPY src/EllieBot.Voice/*.csproj src/EllieBot.Voice/
COPY src/EllieBot.GrpcApiBase/*.csproj src/EllieBot.GrpcApiBase/

# Restore the dependencies for the EllieBot project
RUN dotnet restore src/EllieBot/ -r linux-x64

# Copy the rest of the source code
COPY . .

# Set the working directory to the EllieBot project
WORKDIR /source/src/EllieBot

# Build and publish the EllieBot project, then clean up unnecessary files
RUN dotnet publish -c Release -o /app --self-contained -r linux-x64 --no-restore; \
    mv /app/data /app/data_init; \
    chmod +x /app/EllieBot

# Use the .NET 8.0 runtime as the base image for the final stage
FROM debian:12.9-slim
WORKDIR /app

ADD --chmod=755 https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp /usr/local/bin/yt-dlp

# Create a new user, install dependencies, and set up sudoers file
RUN apt update; \
    apt install -y --no-install-recommends \
        libicu-dev ca-certificates \
        ffmpeg python3; \
    apt autoremove -y; \
    apt clean -y;

RUN update-ca-certificates

# Copy the built application and the entrypoint script from the build stage
COPY --from=build /app ./
COPY docker-entrypoint.sh /usr/local/sbin/

# Define the data directory as a volume
VOLUME [ "/app/data" ]

# Set the entrypoint and default command
ENTRYPOINT [ "/usr/local/sbin/docker-entrypoint.sh" ]
CMD ./EllieBot