name: Generate Documentation

on:
  push:
    branches:
      - main
    paths:
      - 'gaitsetpy/**'
      - 'generate_docs.py'
      - 'requirements.txt'
      - '.github/workflows/docs.yml'
  workflow_dispatch:  # Allow manual trigger

permissions:
  contents: write  # Allow pushing documentation updates

jobs:
  generate-docs:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v5
        with:
          fetch-depth: 0  # Fetch all history for proper git operations
      
      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.11'
      
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install pdoc
          pip install -r requirements.txt
          pip install -e .
      
      - name: Generate documentation
        run: |
          python generate_docs.py
      
      - name: Configure Git
        run: |
          git config --global user.name "GitHub Actions Bot"
          git config --global user.email "actions@github.com"
      
      - name: Check for changes
        id: check_changes
        run: |
          git add -A
          if git diff --staged --quiet; then
            echo "has_changes=false" >> $GITHUB_OUTPUT
            echo "No documentation changes detected"
          else
            echo "has_changes=true" >> $GITHUB_OUTPUT
            echo "Documentation changes detected"
          fi
      
      - name: Commit and push documentation
        if: steps.check_changes.outputs.has_changes == 'true'
        run: |
          git commit -m "Auto-generate documentation [skip ci]"
          git push
      
      - name: Deploy to GitHub Pages
        if: steps.check_changes.outputs.has_changes == 'true'
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: .
          publish_branch: gh-pages
          keep_files: true
          exclude_assets: |
            .github
            .git
            tests
            examples
            __pycache__
            *.py
            *.md
            *.txt
            *.yml
