name: Deploy docs

on:
  push:
    branches: [main]
    paths:
      - 'docs/**'
      - 'notarai.spec.json'
  workflow_dispatch:

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

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

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Install mdBook and mermaid preprocessor
        run: |
          cargo install mdbook mdbook-mermaid

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

      - name: Prepare artifact with redirect
        run: |
          mkdir -p public/docs
          cp -r docs/book/* public/docs/

          cat <<EOF > public/index.html
          <!DOCTYPE html>
          <html>
            <head>
              <meta http-equiv="refresh" content="0; url=./docs/" />
              <link rel="canonical" href="./docs/" />
            </head>
            <body>
              <p>Redirecting to <a href="./docs/">docs</a>...</p>
            </body>
          </html>
          EOF

      - name: Add schemas to public path
        run: |
          mkdir -p public/schema/0.5
          cp schemas/0.5/spec.schema.json public/schema/0.5/spec.schema.json
          mkdir -p public/schema/0.6
          cp schemas/0.6/spec.schema.json public/schema/0.6/spec.schema.json
          mkdir -p public/schema/0.7
          cp notarai.spec.json public/schema/0.7/spec.schema.json

      - uses: actions/upload-pages-artifact@v4
        with:
          path: public

  deploy:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

    steps:
      - id: deployment
        uses: actions/deploy-pages@v4
