name: Update Documentation

on:
  workflow_dispatch:
    inputs:
      version:
        description: Release version (e.g., 4.21.0)
        required: true
        type: string
      language:
        description: language documentation
        required: true
        type: choice
        default: "all"
        options:
          - java
          - rb
          - py
          - dotnet
          - node
          - all

  workflow_call:
    inputs:
      version:
        required: true
        type: string
      sha:
        required: true
        type: string
      language:
        required: false
        type: string
        default: "all"

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  build-docs:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || inputs.sha }}
      - name: Fetch gh-pages branch
        run: git fetch origin gh-pages
      - name: Setup git
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
      - name: Setup curl for Ubuntu
        run: sudo apt-get update && sudo apt-get install -y libcurl4-openssl-dev
      - name: Setup Java
        if: ${{ inputs.language == 'java' || inputs.language == 'all' }}
        uses: actions/setup-java@v4
        with:
          java-version: 17
          distribution: 'temurin'
      - name: Set up Python 3.9
        if: ${{ inputs.language == 'py' || inputs.language == 'all' }}
        uses: actions/setup-python@v5
        with:
          python-version: 3.9
      - name: Install dependencies
        if: ${{ inputs.language == 'py' || inputs.language == 'all' }}
        run: |
          python -m pip install --upgrade pip
          pip install tox
      - name: Install npm dependencies
        if: ${{ inputs.language == 'node' || inputs.language == 'all' }}
        run: |
          npm install
          npm install --prefix javascript/selenium-webdriver
      - name: Generate Documentation for selected langauges
        run: ./go ${{ inputs.language }}:docs
      - name: Documentation Pull Request
        id: cpr
        uses: peter-evans/create-pull-request@v6
        with:
          token: ${{ secrets.SELENIUM_CI_TOKEN }}
          author: Selenium CI Bot <selenium-ci@users.noreply.github.com>
          delete-branch: true
          branch: api-docs-${{ inputs.version }}
          base: gh-pages
          add-paths: docs/api/**
          title: Update documentation for Selenium ${{ inputs.version }}-${{ inputs.language }}
          body: |
            This PR updates the API documentation for version **${{ inputs.version }}-${{ inputs.language }}**.

            - Auto-generated by [workflow run #${{ github.run_id }}](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
          labels: B-docs
          draft: false
      - name: Enable Pull Request Auto-merge
        if: steps.cpr.outputs.pull-request-operation == 'created'
        uses: peter-evans/enable-pull-request-automerge@v3
        with:
          token: ${{ secrets.SELENIUM_CI_TOKEN }}
          pull-request-number: ${{ steps.cpr.outputs.pull-request-number }}
          merge-method: squash
