name: Build and deploy documentation

on:
    push:
        branches:
            - main
        paths:
            - 'docs/**'
            - 'mkdocs.yml'
            - '.github/workflows/docs.yaml'
    pull_request:
        branches:
            - main
        paths:
            - 'docs/**'
            - 'mkdocs.yml'
            - '.github/workflows/docs.yaml'
    # Manual trigger
    workflow_dispatch:
    # Daily deployment to catch any release notes added by auto-release
    schedule:
        # Run at 2 AM UTC daily (change time as needed)
        - cron: '0 2 * * *'

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

# Allow only one concurrent deployment
concurrency:
    group: 'pages'
    cancel-in-progress: false

jobs:
    build-docs:
        runs-on: ubuntu-latest
        name: Build documentation

        defaults:
            run:
                shell: bash -l {0}

        steps:
            - name: Checkout repository
              uses: actions/checkout@v6
              with:
                  fetch-depth: 0

            - name: Setup Pixi Environment
              uses: prefix-dev/setup-pixi@v0.9.4
              with:
                  pixi-version: latest
                  cache: true
                  cache-write: ${{ github.event_name == 'push' && github.ref_name == 'main' }}

            - name: Build docs
              run: pixi run -e docs docs-build

            - name: Verify build output
              run: |
                  echo "✓ Site directory contents:"
                  ls -la ./site
                  echo "✓ Build verification passed"

            - name: Upload artifact
              if: github.event_name == 'push' && github.ref_name == 'main'
              uses: actions/upload-pages-artifact@v4
              with:
                  path: ./site

    deploy:
        if: github.event_name == 'push' && github.ref_name == 'main'
        runs-on: ubuntu-latest
        needs: build-docs
        name: Deploy to GitHub Pages

        environment:
            name: github-pages
            url: ${{ steps.deployment.outputs.page_url }}

        steps:
            - name: Deploy to GitHub Pages
              id: deployment
              uses: actions/deploy-pages@v4
