name: Deploy Documentation

on:
  push:
    branches:
      - main
    paths:
      - 'docs/book/**'
      - '.github/workflows/docs.yml'

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

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

      - name: Install mdbook-mermaid
        run: |
          mkdir -p bin
          curl -LSs https://github.com/badboy/mdbook-mermaid/releases/latest/download/mdbook-mermaid-v0.14.0-x86_64-unknown-linux-gnu.tar.gz | tar -xz -C bin
          echo "$(pwd)/bin" >> $GITHUB_PATH

      - name: Build with mdBook
        run: |
          mdbook-mermaid install docs/book
          mdbook build docs/book

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        if: github.ref == 'refs/heads/main'
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./docs/book/book
          publish_branch: gh-pages
