name: docs-version

on:
  push:
    tags: ["v*"]

permissions:
  contents: write

concurrency:
  group: docs-version-${{ github.ref }}
  cancel-in-progress: true

jobs:
  version-docs:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout main
        uses: actions/checkout@v4
        with:
          ref: main
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: "npm"
          cache-dependency-path: package-lock.json

      - name: Install dependencies
        run: npm ci --workspaces --include-workspace-root
        working-directory: .

      - name: Derive docs version from tag
        id: vars
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          if [ -z "$VERSION" ]; then
            echo "Unable to derive version from tag '$GITHUB_REF_NAME'" >&2
            exit 1
          fi
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

      - name: Generate docs version
        run: npm run docs:version -- "${{ steps.vars.outputs.version }}"
        working-directory: .

      - name: Format generated docs files
        run: |
          ./orchestrator/node_modules/.bin/biome check --write \
            docs-site/versions.json \
            docs-site/versioned_docs \
            docs-site/versioned_sidebars

      - name: Commit and push generated docs version files
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

          git add docs-site/versions.json docs-site/versioned_docs docs-site/versioned_sidebars

          if git diff --cached --quiet; then
            echo "No docs version changes detected; skipping commit."
            exit 0
          fi

          git commit -m "docs: cut version ${{ steps.vars.outputs.version }}"
          git push origin HEAD:main
