name: Documentation Sync

on:
  pull_request:
    branches: [ master ]
    paths:
      - 'src/**/*.rs'
      - 'docs/**/*.md'

permissions:
  contents: write

jobs:
  sync-docs:
    runs-on: ubuntu-latest
    steps:
      - name: Generate GitHub App token
        uses: actions/create-github-app-token@v2
        id: generate-token
        with:
          app-id: ${{ secrets.GH_APP_ID }}
          private-key: ${{ secrets.GH_APP_PRIVATE_KEY }}
          owner: DecapodLabs
          
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
          fetch-depth: 0
          token: ${{ steps.generate-token.outputs.token }}
          persist-credentials: false

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Build Decapod
        env:
          CARGO_NET_GIT_FETCH_WITH_CLI: "true"
          GIT_TERMINAL_PROMPT: "0"
        run: cargo build --all-features

      - name: Get changed files
        id: changed-files
        run: |
          git fetch origin master
          FILES=$(git diff --name-only origin/master HEAD | xargs)
          echo "files=$FILES" >> $GITHUB_OUTPUT

      - name: Run Docs Build
        run: |
          FILES="${{ steps.changed-files.outputs.files }}"
          if [ -n "$FILES" ]; then
            ./target/debug/decapod docs build --touched $FILES
          else
            ./target/debug/decapod docs build
          fi

      - name: Commit and push changes
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"
          git remote set-url origin https://x-access-token:${{ steps.generate-token.outputs.token }}@github.com/${{ github.repository }}
          git add docs/agent/command-contracts.md docs/agent/config-schema.md
          if git diff --staged --quiet; then
            echo "No doc changes detected."
          else
            git commit -m "docs: autogenerate/sync command contracts"
            git push
          fi
