# ─────────────────────────────────────────────────────────────────────────────
# FinlyticNews — Application Dockerfile
#
# Prerequisite: The Playwright base image must exist locally.
#   docker build -f FinlyticNews/Dockerfile.playwright-base `
#                -t finlytic-playwright-base:1.49.0 `
#                FinlyticNews
#
# Then build this image as normal:
#   docker compose build finlyticnews
#   — or —
#   docker build -f FinlyticNews/Dockerfile -t finlyticnews .
# ─────────────────────────────────────────────────────────────────────────────

# ── Stage 1: Build ────────────────────────────────────────────────────────────
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src

# Restore dependencies first (cached until .csproj changes)
COPY ["FinlyticNews/FinlyticNews.csproj", "FinlyticNews/"]
COPY ["FinlyticCore/FinlyticCore.csproj", "FinlyticCore/"]
RUN dotnet restore "FinlyticNews/FinlyticNews.csproj"

# Build
COPY . .
WORKDIR "/src/FinlyticNews"
RUN dotnet build "./FinlyticNews.csproj" -c $BUILD_CONFIGURATION -o /app/build

# ── Stage 2: Publish ──────────────────────────────────────────────────────────
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
# PlaywrightCopyPlaywrightFilesToOutput copies the Node driver + CLI into /app/publish/.playwright
# We only need this to get the playwright CLI script; browsers come from the base image.
RUN dotnet publish "./FinlyticNews.csproj" \
        -c $BUILD_CONFIGURATION \
        -o /app/publish \
        /p:UseAppHost=false \
        /p:PlaywrightCopyPlaywrightFilesToOutput=true

# ── Stage 3: Final ────────────────────────────────────────────────────────────
# Use the pre-built base image that already has Chromium + all OS dependencies.
# This layer is cached on Docker Desktop and NOT re-downloaded on code changes.
FROM finlytic-playwright-base:1.49.0 AS final

USER root
WORKDIR /app

# Copy published application
COPY --from=publish /app/publish .

# The Playwright CLI + Node driver are published into .playwright by the build above.
# Verify the driver is present (sanity check — doesn't install anything).
RUN test -d /app/.playwright && \
    test -f /app/.playwright/node/linux-x64/node && \
    echo "✅ Playwright driver present"

# Browsers are already in /opt/ms-playwright from the base image — nothing to download.
ENV PLAYWRIGHT_BROWSERS_PATH=/opt/ms-playwright

# Fix ownership
RUN chown -R $APP_UID:$APP_UID /app /opt/ms-playwright

USER $APP_UID

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