name: Update API Documentation
on:
  push:
    branches:
      - main
    paths:
      - 'api/docs/**'
  workflow_dispatch:

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

# Add permissions for GITHUB_TOKEN
permissions:
  contents: write
  pull-requests: write
jobs:
  create-docs-pr:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source repository
        uses: actions/checkout@v4
        with:
          path: source-repo

      - name: Checkout docs repository
        uses: actions/checkout@v4
        with:
          repository: unraid/docs
          path: docs-repo
          token: ${{ secrets.DOCS_PAT_UNRAID_BOT }}

      - name: Copy updated docs
        run: |
          if [ ! -d "source-repo/api/docs" ]; then
            echo "Source directory does not exist!"
            exit 1
          fi
          rm -rf docs-repo/docs/API/
          mkdir -p docs-repo/docs/API
          cp -r source-repo/api/docs/public/. docs-repo/docs/API/
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v7
        with:
          token: ${{ secrets.DOCS_PAT_UNRAID_BOT }}
          path: docs-repo
          commit-message: 'docs: update API documentation'
          title: 'Update API Documentation'
          body: |
            This PR updates the API documentation based on changes from the main repository.
            
            Changes were automatically generated from api/docs/* directory.

            @coderabbitai ignore
          reviewers: ljm42, elibosley, pujitm, mdatelle
          branch: update-api-docs
          base: main
          delete-branch: true
