name: Docs

on:
  push:
    branches: [ master ]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

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

jobs:
  build:
    name: Build Sphinx HTML
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.x"
          cache: 'pip'

      - name: Install docs dependencies
        run: |
          python -m pip install --upgrade pip
          # Prefer a dedicated docs extra if you have it:
          pip install .[docs] || true
          # Fallbacks (adjust to your project):
          pip install sphinx sphinx-autobuild furo \
                     sphinx-copybutton sphinx-design \
                     sphinxcontrib-jquery \
                     sphinxext-opengraph \
                     myst-parser \
                     pillow \
                     matplotlib || true

      - name: Build HTML
        working-directory: docs
        run: |
          # Common Sphinx layouts: docs/source -> _build/html OR docs/ -> _build/html
          if [ -d "source" ]; then
            sphinx-build -b html source _build/html
          else
            sphinx-build -b html . _build/html
          fi

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v3
        with:
          # Match the build path above
          path: docs/_build/html

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