name: Documentation

on:
  push:
    branches: [main]
    paths:
      - src/**
      - docs/**
      - README.md
      - index.html
      - typedoc.json
      - .github/workflows/docs.yml
  workflow_dispatch:

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

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

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v5

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install dependencies
        run: bun install

      # typedoc.json: API reference + docs/HLD.md, with docs/assets/mermaid.js
      # rendering the Mermaid diagrams. Same command as `fledge run docs`.
      - name: Generate documentation
        run: bunx typedoc@0.28

      - name: Check the HLD page and its diagrams were generated
        run: grep -q 'class="mermaid"' _site/documents/High-Level_Design.html

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

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