name: Documentation
# Builds on pushes to develop and all pull requests
# Deploys only on push events
on:
  push:
    branches:
      - develop
  pull_request:  # all pull requests

jobs:
  Build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # fetch the entire repo history, required to guarantee versioneer will pick up the tags
      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
      - run: python -m venv pycalphad-env
      - run: source pycalphad-env/bin/activate
      - run: pip install -U pip setuptools
      - run: pip install -U -r requirements-dev.txt
      - run: pip install -U --no-build-isolation --editable .
      - run: sudo wget https://github.com/jgm/pandoc/releases/download/2.18/pandoc-2.18-1-amd64.deb
      - run: sudo dpkg -i pandoc-2.18-1-amd64.deb
      # Build documentation
      - run: sphinx-build -W -b html docs docs/_build/html
      # Upload documentation html directory
      - uses: actions/upload-artifact@v4
        with:
          path: ./docs/_build/html
          name: pycalphad-documentation

  Deploy-Netlify:
    name: Deploy to Netlify
    needs: [Build]
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' }}
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: pycalphad-documentation
          path: pycalphad-docs-html
      - name: Deploy to Netlify
        uses: nwtgck/actions-netlify@v3.0
        with:
          publish-dir: 'pycalphad-docs-html'
          production-branch: develop
          github-token: ${{ secrets.GITHUB_TOKEN }}
          deploy-message: "Deploy from GitHub Actions"
          enable-pull-request-comment: true
          enable-commit-comment: true
          overwrites-pull-request-comment: true
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
        timeout-minutes: 1

  Deploy-latest:
    name: Deploy to `latest` on `website` branch
    needs: [Build]
    runs-on: ubuntu-latest
    # Deploy to "latest" docs on the website branch on pushes
    # Pushes are limited to only run on the `develop` branch (at the top)
    if: ${{ github.event_name == 'push' }}
    env:
      TARGET_BRANCH: website
      DEPLOY_NAME: latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # fetch the entire repo history, required to guarantee the TARGET_BRANCH exists
      - uses: actions/download-artifact@v4
        with:
          name: pycalphad-documentation
          path: pycalphad-docs-html
      - run: git checkout $TARGET_BRANCH
      - name: Copy Documentation to destination
        run: |
          mkdir -p docs/$DEPLOY_NAME
          # clean any existing contents
          rm -rf docs/$DEPLOY_NAME/*
          # copy the output
          cp -Rf pycalphad-docs-html/* docs/$DEPLOY_NAME
      - name: Commit and push changes
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add docs/$DEPLOY_NAME
          git commit -m 'DOC: Deploy ${{ env.DEPLOY_NAME }} docs to website: ${{ github.sha }}'
          git push
