name: Documentation

on:
  push:
    branches: [ main ]
    paths:
      - 'docs/**'
      - 'stac_validator/**/*.py'
      - 'README.md'
  pull_request:
    branches: [ main ]
    paths:
      - 'docs/**'
      - 'stac_validator/**/*.py'
      - 'README.md'
  # Allow manual triggering
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.8'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -e .
        pip install sphinx sphinx_rtd_theme myst-parser sphinx-autodoc-typehints
    - name: Build documentation
      run: |
        sphinx-build -b html docs/ docs/_build/html
    - name: Upload documentation artifact
      uses: actions/upload-artifact@v4
      with:
        name: documentation
        path: docs/_build/html
        retention-days: 7

  # Only deploy when pushing to main (not on PRs)
  deploy:
    needs: build
    if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download built documentation
        uses: actions/download-artifact@v4
        with:
          name: documentation
          path: ./docs-build
      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./docs-build
          force_orphan: true
          commit_message: "Update documentation [skip ci]"
