name: Deploy Docs

on:
  # push:
  #   branches: [main]
  workflow_dispatch:

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

concurrency:
  group: pages
  cancel-in-progress: false

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

      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable

      - uses: Swatinem/rust-cache@v2

      - name: Build API docs
        run: cargo doc --no-deps --all-features

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

      - name: Install mermaid assets
        working-directory: docs
        run: mdbook-mermaid install

      - name: Build book
        working-directory: docs
        run: mdbook build

      - name: Assemble site
        run: |
          mkdir -p _site
          cp -r docs/book/* _site/
          mkdir -p _site/api
          cp -r target/doc/lspz/* _site/api/
          echo '<meta http-equiv="refresh" content="0;url=api/lspz/index.html">' > _site/api/index.html

      - uses: actions/upload-pages-artifact@v3
        with:
          path: _site

  deploy:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
