name: Deploy Docs
on:
  push:
    branches: [master]
    paths: [docs/**]
  workflow_dispatch:

jobs:
  deploy-docs:
    runs-on: ubuntu-latest

    permissions:
      contents: write
      pages: write
      id-token: write

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

      - name: Set up Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1

      - name: Install mdbook
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          jq_expr='.assets[] | select(.name | contains("x86_64-unknown-linux-gnu")) | .browser_download_url'
          url=$(gh api repos/rust-lang/mdbook/releases/tags/v0.4.52 --jq "$jq_expr")
          mkdir mdbook
          curl -sSL "$url" | tar -xz -C mdbook
          printf '%s/mdbook\n' "$PWD" >> "$GITHUB_PATH"

      - name: Install mdbook-admonish
        run: cargo install mdbook-admonish

      - name: Build Book
        run: mdbook build docs

      - name: Setup Pages
        uses: actions/configure-pages@v5

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v4
        with:
          path: docs/book

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
