name: Publish Reports to GitHub Pages

on:
  push:
    branches:
      - main
    paths:
      - 'src/main/**'
      - 'src/test/**'
      - 'src/jmh/**'
      - 'build.gradle'
      - 'settings.gradle'
      - 'gradle/**'
      - 'gradlew'
      - 'gradlew.bat'
      - '.github/workflows/pages.yml'
  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: pages-${{ github.ref }}
  cancel-in-progress: true

jobs:
  publish-pages:
    name: Publish static reports
    runs-on: ubuntu-latest

    steps:
      - name: Check out source repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Validate Gradle wrapper
        uses: gradle/actions/wrapper-validation@v4

      - name: Set up Temurin JDK 21
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: '21'

      - name: Set up Gradle caching and instrumentation
        uses: gradle/actions/setup-gradle@v4

      - name: Build reports for publication
        run: ./gradlew --no-daemon clean build pmdMain javadoc jacocoTestReport pitest jmh

      - name: Prepare gh-pages worktree
        shell: bash
        run: |
          set -euo pipefail

          if git ls-remote --exit-code --heads origin gh-pages > /dev/null 2>&1; then
            git fetch origin gh-pages:gh-pages
            git worktree add .gh-pages gh-pages
          else
            git worktree add --detach .gh-pages
            cd .gh-pages
            git checkout --orphan gh-pages
            git rm -rf . > /dev/null 2>&1 || true
            cd ..
          fi

      - name: Stage published reports
        shell: bash
        run: |
          set -euo pipefail

          SITE_DIR=".gh-pages"
          RUN_DIR="${SITE_DIR}/builds/${GITHUB_RUN_NUMBER}"
          LATEST_DIR="${SITE_DIR}/builds/latest"

          mkdir -p "${RUN_DIR}"
          rm -rf "${LATEST_DIR}"
          mkdir -p "${LATEST_DIR}"

          cp -R build/docs/javadoc "${RUN_DIR}/javadoc"
          cp -R build/docs/javadoc "${LATEST_DIR}/javadoc"

          cp -R build/reports/tests/test "${RUN_DIR}/test"
          cp -R build/reports/tests/test "${LATEST_DIR}/test"

          cp -R build/reports/pmd "${RUN_DIR}/pmd"
          cp -R build/reports/pmd "${LATEST_DIR}/pmd"

          cp -R build/reports/jacoco/test/html "${RUN_DIR}/coverage"
          cp -R build/reports/jacoco/test/html "${LATEST_DIR}/coverage"

          cp -R build/reports/pitest "${RUN_DIR}/pitest"
          cp -R build/reports/pitest "${LATEST_DIR}/pitest"

          JMH_TXT_LINK=''
          JMH_CSV_LINK=''
          JMH_TXT_LATEST_LINK=''
          JMH_CSV_LATEST_LINK=''

          if [ -d "build/reports/jmh" ]; then
            cp -R build/reports/jmh "${RUN_DIR}/jmh"
            cp -R build/reports/jmh "${LATEST_DIR}/jmh"

            if [ -f "${RUN_DIR}/jmh/jmh-results.txt" ]; then
              JMH_TXT_LINK='<li><a href="./jmh/jmh-results.txt">Benchmark Results (TXT)</a></li>'
              JMH_TXT_LATEST_LINK='<li><a href="./builds/latest/jmh/jmh-results.txt">Benchmark Results (TXT)</a></li>'
            fi
            if [ -f "${RUN_DIR}/jmh/jmh-results.csv" ]; then
              JMH_CSV_LINK='<li><a href="./jmh/jmh-results.csv">Benchmark Results (CSV)</a></li>'
              JMH_CSV_LATEST_LINK='<li><a href="./builds/latest/jmh/jmh-results.csv">Benchmark Results (CSV)</a></li>'
            fi

            HAS_JMH="true"
          else
            HAS_JMH="false"
          fi

          cat > "${RUN_DIR}/index.html" <<EOF
          <!doctype html>
          <html lang="en">
          <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Radixor Reports - Build ${GITHUB_RUN_NUMBER}</title>
            <style>
              body { font-family: Arial, sans-serif; max-width: 900px; margin: 2rem auto; padding: 0 1rem; line-height: 1.5; }
              h1, h2 { margin-bottom: 0.5rem; }
              ul { padding-left: 1.25rem; }
              code { background: #f4f4f4; padding: 0.1rem 0.3rem; }
              .meta { color: #555; }
            </style>
          </head>
          <body>
            <h1>Radixor Reports</h1>
            <p class="meta">Build ${GITHUB_RUN_NUMBER} from commit ${GITHUB_SHA}</p>
            <ul>
              <li><a href="./javadoc/">Javadoc</a></li>
              <li><a href="./test/">Test Report</a></li>
              <li><a href="./pmd/main.html">PMD Report</a></li>
              <li><a href="./coverage/">Coverage Report</a></li>
              <li><a href="./pitest/">Mutation Testing Report</a></li>
              $(
                [ "${HAS_JMH}" = "true" ] && { echo "${JMH_TXT_LINK:-<li>Benchmark Results (TXT): not available</li>}"; echo "${JMH_CSV_LINK:-<li>Benchmark Results (CSV): not available</li>}"; } \
                || echo '<li>Benchmark results: not available in this build</li>'
              )
            </ul>
            <p><a href="../latest/">Open latest report set</a></p>
            <p><a href="../../">Back to report home</a></p>
          </body>
          </html>
          EOF

          cp "${RUN_DIR}/index.html" "${LATEST_DIR}/index.html"

          cat > "${SITE_DIR}/.nojekyll" <<EOF
          EOF

          BUILD_LIST=$(find "${SITE_DIR}/builds" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | grep -E '^[0-9]+$' | sort -nr | head -20)

          {
            cat <<EOF
          <!doctype html>
          <html lang="en">
          <head>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Radixor Reports</title>
            <style>
              body { font-family: Arial, sans-serif; max-width: 1000px; margin: 2rem auto; padding: 0 1rem; line-height: 1.5; }
              h1, h2 { margin-bottom: 0.5rem; }
              ul { padding-left: 1.25rem; }
              code { background: #f4f4f4; padding: 0.1rem 0.3rem; }
              .meta { color: #555; }
            </style>
          </head>
          <body>
            <h1>Radixor Published Reports</h1>
            <p class="meta">Durable CI reports published from GitHub Actions to the <code>gh-pages</code> branch.</p>

            <h2>Latest</h2>
            <ul>
              <li><a href="./builds/latest/">Latest build summary</a></li>
              <li><a href="./builds/latest/javadoc/">Javadoc</a></li>
              <li><a href="./builds/latest/test/">Test Report</a></li>
              <li><a href="./builds/latest/pmd/main.html">PMD Report</a></li>
              <li><a href="./builds/latest/coverage/">Coverage Report</a></li>
              <li><a href="./builds/latest/pitest/">Mutation Testing Report</a></li>
              $(
                [ "${HAS_JMH}" = "true" ] && { echo "${JMH_TXT_LATEST_LINK:-<li>Benchmark Results (TXT): not available</li>}"; echo "${JMH_CSV_LATEST_LINK:-<li>Benchmark Results (CSV): not available</li>}"; } \
                || echo '<li>Benchmark results: not currently available</li>'
              )
          EOF

            cat <<EOF
            </ul>

            <h2>Recent historical builds</h2>
            <ul>
          EOF

            for build in ${BUILD_LIST}; do
              echo "      <li><a href=\"./builds/${build}/\">Build ${build}</a></li>"
            done

            cat <<EOF
            </ul>
          </body>
          </html>
          EOF
          } > "${SITE_DIR}/index.html"

      - name: Commit and push gh-pages
        shell: bash
        run: |
          set -euo pipefail
          cd .gh-pages

          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

          git add .
          if git diff --cached --quiet; then
            echo "No changes to publish."
            exit 0
          fi

          git commit -m "Publish reports for run ${GITHUB_RUN_NUMBER}"
          git push origin gh-pages
