name: Lint and Generate Documentation

on:
  push:
    branches:
      - main

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
      with:
        persist-credentials: false

    - name: Set up Python
      uses: actions/setup-python@v3
      with:
        python-version: '3.9'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install ruff

    - name: Run Ruff
      run: |
        ruff check .
        ruff format .

    - name: Commit linting changes
      run: |
        git config --local user.email "github-actions[bot]@users.noreply.github.com"
        git config --local user.name "github-actions[bot]"
        git add .
        git commit -m "Apply automatic linting" || echo "No changes to commit"

    - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GH_TOKEN }}
        branch: ${{ github.ref }}

  generate-docs:
    needs: lint
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3
      with:
        persist-credentials: false

    - name: Set up Python
      uses: actions/setup-python@v3
      with:
        python-version: '3.9'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Generate documentation
      run: |
        pip install pdoc3
        mkdir -p docs
        python -m pdoc --html --output-dir docs transcripter --force

    - name: Commit documentation changes
      run: |
        git config --local user.email "github-actions[bot]@users.noreply.github.com"
        git config --local user.name "github-actions[bot]"
        git add docs
        git commit -m "Update documentation" || echo "No changes to commit"

    - name: Push changes
      uses: ad-m/github-push-action@v0.8.0
      with:
        github_token: ${{ secrets.GH_TOKEN }}
        branch: ${{ github.ref }}