name: Documentation

on:
  push:
    branches: [main]
    paths:
      - 'docs/book/**'
      - 'src/**'
      - 'Cargo.toml'
      - '.github/workflows/docs.yml'
  pull_request:
    branches: [main]
    paths:
      - 'docs/book/**'
      - '.github/workflows/docs.yml'
  workflow_dispatch:

# Allow only one concurrent deployment
concurrency:
  group: pages-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install mdBook
        uses: peaceiris/actions-mdbook@v2
        with:
          mdbook-version: 'latest'

      - name: Build book
        run: mdbook build docs/book

      - name: Build API docs
        run: cargo doc --no-deps --document-private-items
        env:
          RUSTDOCFLAGS: "--default-theme ayu"

      - name: Merge docs
        run: |
          mkdir -p docs/book/book/api
          cp -r target/doc/* docs/book/book/api/

      - name: Copy LLM files
        run: |
          cp docs/llms.txt docs/book/book/llms.txt
          cp docs/llms-full.txt docs/book/book/llms-full.txt

      - name: Merge benchmark dashboard from gh-pages
        run: |
          git fetch origin gh-pages:gh-pages || true
          if git rev-parse --verify gh-pages >/dev/null 2>&1; then
            git show gh-pages:dev/bench/index.html > /dev/null 2>&1 && {
              mkdir -p docs/book/book/dev/bench
              git show gh-pages:dev/bench/index.html > docs/book/book/dev/bench/index.html
              git show gh-pages:dev/bench/data.js > docs/book/book/dev/bench/data.js
            } || echo "No benchmark data on gh-pages yet"
          else
            echo "gh-pages branch not found, skipping benchmark merge"
          fi

      - name: Upload artifact
        if: github.ref == 'refs/heads/main'
        uses: actions/upload-pages-artifact@v3
        with:
          path: docs/book/book

  link-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install mdBook
        uses: peaceiris/actions-mdbook@v2
        with:
          mdbook-version: 'latest'

      - name: Build book
        run: mdbook build docs/book

      - name: Link checker
        uses: lycheeverse/lychee-action@v2
        with:
          args: >-
            --verbose
            --no-progress
            --accept 200,204,301,302,429
            --exclude 'github\.com/.*/edit/'
            --exclude 'localhost'
            --exclude 'adobe\.com'
            --exclude 'scylladb\.github\.io/cqlsh-rs/dev/bench'
            --exclude-path 'docs/book/book/404.html'
            --exclude-path 'docs/book/book/fonts'
            docs/book/book
          fail: true

  deploy:
    if: github.ref == 'refs/heads/main'
    needs: [build, link-check]
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
