name: Generate Documentation

on:
  release:
    types: [published]
  push:
    branches:
      - main
  pull_request:

permissions: {}

jobs:
  build:
    permissions:
      contents: write # for git push

    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.10"]
    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        fetch-depth: 0
    - name: Checkout statsmodels.github.io without token
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        repository: statsmodels/statsmodels.github.io
        path: statsmodels.github.io
      if: ${{ github.event_name == 'pull_request' }}
    - name: Checkout statsmodels.github.io with token
      uses: actions/checkout@v4
      with:
        fetch-depth: 1
        repository: statsmodels/statsmodels.github.io
        path: statsmodels.github.io
        token: ${{ secrets.DOCBUILD_TOKEN }}
      if: ${{ github.event_name == 'release' || github.event_name == 'push' }}
    - name: Event type
      run: echo $EVENT_TYPE
      env:
        EVENT_TYPE: ${{ github.event_name }}
    - name: Install pandoc
      uses: r-lib/actions/setup-pandoc@v2
    - name: Install graphviz
      uses: ts-graphviz/setup-graphviz@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v5
      with:
        python-version: ${{ matrix.python-version }}
        cache: "pip"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip wheel setuptools
        python -m pip install -r requirements.txt
        python -m pip install -r requirements-dev.txt
        python -m pip install -r requirements-doc.txt
        python -m pip list
    - name: Install statsmodels
      run: python -m pip install . -vv
    - name: Build documentation
      run: |
        pushd docs
        make html
        popd
    - name: Move and commit documentation
      env:
        GIT_TAG: ${{ github.event.release.tag_name }}
      run: |
        source tools/ci/docbuild-commit.sh
    - name: Push changes
      env:
        PERSONAL_ACCESS_TOKEN: ${{ secrets.DOCBUILD_TOKEN }}
      run: |
        source tools/ci/docbuild-push.sh
      if: ${{ github.event_name == 'release' || github.event_name == 'push' }}
