name: Build and Deploy Documentation

# This workflow builds the documentation site and deploys it to GitHub Pages
# It runs when changes are pushed to the 'docs' folder on the main branch

on:
  push:
    branches: [main]
    paths:
      - 'docs/**'
      - 'semantica/**'
      - 'mkdocs.yml'
      - 'requirements-docs.txt'
      - 'CHANGELOG.md'
      - 'RELEASE.md'
  workflow_dispatch:

# Permissions needed to deploy to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Prevent concurrent deployments
concurrency:
  group: "pages"
  cancel-in-progress: false

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

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install documentation dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements-docs.txt

      - name: Build documentation
        # Builds the static site using MkDocs
        run: mkdocs build --strict

      - name: Check for broken links
        # Optional: checks if any links in the docs are broken
        run: |
          pip install linkchecker || echo "Skipping link check"
          if [ -d "site" ]; then
            linkchecker site/ --check-extern || echo "Link check completed"
          fi
        continue-on-error: true

      - name: Setup Pages
        uses: actions/configure-pages@v4
        continue-on-error: true

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./site

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