name: Test Virgin Root

on:
  workflow_call:

jobs:
  test-virgin-root:
    runs-on: ubuntu-latest
    timeout-minutes: 45
    strategy:
      fail-fast: false
      matrix:
        distro: [arch, debian, ubuntu, fedora, centos]

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Show Docker version
        run: docker version

      # 🔹 BUILD virgin image if missing
      - name: Build virgin container (${{ matrix.distro }})
        run: |
          set -euo pipefail
          PKGMGR_DISTRO="${{ matrix.distro }}" make build-missing-virgin

      # 🔹 RUN test inside virgin image
      - name: Virgin ${{ matrix.distro }} pkgmgr test (root)
        run: |
          set -euo pipefail

          docker run --rm \
            -v "$PWD":/src \
            -v pkgmgr_repos:/root/Repositories \
            -v pkgmgr_pip_cache:/root/.cache/pip \
            -w /src \
            "pkgmgr-${{ matrix.distro }}-virgin" \
            bash -lc '
              set -euo pipefail

              git config --global --add safe.directory /src

              make install
              make setup

              . "$HOME/.venvs/pkgmgr/bin/activate"

              export NIX_CONFIG="experimental-features = nix-command flakes"

              pkgmgr update pkgmgr --clone-mode shallow --no-verification
              pkgmgr version pkgmgr

              echo ">>> Running Nix-based: nix run .#pkgmgr -- version pkgmgr"
              nix run /src#pkgmgr -- version pkgmgr
            '
