name: Deploy docs

on:
  push:
    branches:
      - stable
  workflow_dispatch:
    inputs:
      ref:
        description: "Ref to deploy, defaults to `unstable`"
        required: false
        default: "unstable"
        type: string

jobs:
  docs:
    runs-on: ubuntu-latest
    env:
      DEPLOY_REF: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.ref || 'stable' }}
    steps:
      # Log out the ref being deployed
      - name: Log Deployment Ref
        if: github.event_name == 'workflow_dispatch'
        run: |
          echo "Deploying ref: $DEPLOY_REF"

      # Checkout the correct ref being deployed
      - uses: actions/checkout@v4
        with:
          ref: ${{ env.DEPLOY_REF }}

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          check-latest: true
          cache: yarn

      - name: Node.js version
        id: node
        run: echo "v8CppApiVersion=$(node --print "process.versions.modules")" >> $GITHUB_OUTPUT

      - name: Restore dependencies
        uses: actions/cache@master
        id: cache-deps
        with:
          path: |
            node_modules
            packages/*/node_modules
          key: ${{ runner.os }}-${{ steps.node.outputs.v8CppApiVersion }}-${{ hashFiles('**/yarn.lock', '**/package.json') }}

      - name: Install & build
        if: steps.cache-deps.outputs.cache-hit != 'true'
        run: yarn install --frozen-lockfile && yarn build

      - name: Build
        run: yarn build
        if: steps.cache-deps.outputs.cache-hit == 'true'

      - name: Build and collect docs
        run: yarn docs:build

      - name: Lint built docs
        run: yarn docs:lint:fix

      - name: Build docs
        working-directory: docs
        run: yarn && yarn build

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./docs/build
