name: Build & Deploy Sphinx Docs

on:
  push:
    branches: [ main ]
    paths:
      - ".github/workflows/docs.yml"
      - "src/**"
      - "docs/**"
      - "pyproject.toml"
      - "README.md"
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: "pages"
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      LANG: C.UTF-8
      LC_ALL: C.UTF-8
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Configure GitHub Pages
        uses: actions/configure-pages@v5

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.13"
          cache: "pip"

      - name: Install pandoc
        run: |
          sudo apt-get update
          sudo apt-get install -y pandoc

      - name: Install package + doc deps
        run: |
          python -m pip install -U pip
          pip install -e '.[docs]'

      - name: Generate API stubs
        run: |
          sphinx-apidoc -o docs/source/api src/fermilink -f -e -M

      - name: Clean previous doc build
        run: rm -rf docs/build

      - name: Build HTML
        run: |
          sphinx-build -b html -d docs/build/doctrees docs/source docs/build/html

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v4
        with:
          path: docs/build/html

  deploy:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4
