name: 0. Docs (manual)

on:
  # push:
  #   branches: [ "main" ]
  #   paths:
  #     - 'docs/**'
  #     - 'src/**'
  #     - 'pyproject.toml'
  #     - 'uv.lock'
  #     - '.github/workflows/docs.yml'
  workflow_dispatch:

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

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

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

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.13'

      - name: Setup uv
        uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true

      - name: Sync dependencies (default + dev)
        shell: bash
        run: |
          uv python install 3.13
          uv venv --python 3.13
          source .venv/bin/activate
          # Install project default deps and dev toolchain (Sphinx, theme, etc.)
          uv sync --all-groups

      - name: Install optional extras for autodoc imports (gui, web)
        shell: bash
        run: |
          source .venv/bin/activate
          # Ensure optional imports (PyQt5, FastAPI, etc.) don't break autodoc
          uv pip install -e '.[gui,web]'

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

      - name: Build Sphinx API and HTML
        shell: bash
        env:
          # Use the workflow's GITHUB_TOKEN for GitHub API calls in sphinx-github-changelog
          SPHINX_GITHUB_CHANGELOG_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set -euo pipefail
          source .venv/bin/activate
          SPHINXOPTS="-j auto" make -C docs -j"$(nproc)" api html

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v3
        with:
          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
