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

# Restore dependencies
COPY SaasMediaWorker.csproj .
RUN dotnet restore

# Build
COPY . .
RUN dotnet publish -c Release -o /app/publish --no-restore

# Runtime — ARM64 uyumlu Alpine image
FROM mcr.microsoft.com/dotnet/runtime:8.0-alpine AS runtime
WORKDIR /app

# FFmpeg, Node.js (Remotion için) ve Chromium (Puppeteer/Remotion için) kurulumu
RUN apk add --no-cache ffmpeg font-dejavu icu-libs nodejs npm chromium nss freetype harfbuzz ttf-freefont
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false

# Temp dizin oluştur
RUN mkdir -p /tmp/contgen-render

COPY --from=build /app/publish .

# Remotion projesini kopyala ve bağımlılıkları kur
COPY remotion ./remotion
RUN cd remotion && npm ci

# Non-root user ile çalıştır (güvenlik)
RUN adduser -D -h /app workeruser && \
    chown -R workeruser:workeruser /app/remotion && \
    chown -R workeruser:workeruser /tmp/contgen-render

USER workeruser

ENTRYPOINT ["dotnet", "SaasMediaWorker.dll"]
