name: Deploy to GitHub Pages

on:
  push:
    branches: [site, main]
  workflow_dispatch:

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

concurrency:
  group: pages
  cancel-in-progress: false

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: Install uv
        uses: astral-sh/setup-uv@v7
        with:
          python-version: "3.14t"

      - name: Cache uv dependencies
        uses: actions/cache@v5
        with:
          path: ~/.cache/uv
          key: uv-${{ runner.os }}-${{ hashFiles('uv.lock') }}
          restore-keys: |
            uv-${{ runner.os }}-

      - name: Install bengal
        run: uv sync --no-sources --extra search
        env:
          PYTHON_GIL: "0"

      # Use Bengal's own input discovery so CI cache invalidation matches build semantics.
      - name: Get Bengal cache hash
        id: bengal-cache
        run: echo "hash=$(uv run bengal cache hash --source site)" >> $GITHUB_OUTPUT
        env:
          PYTHON_GIL: "0"

      - name: Cache Bengal build state
        uses: actions/cache@v5
        with:
          path: |
            .bengal/cache
            .bengal/content_cache
            .bengal/indexes
            .bengal/provenance
            .bengal/version-builds/manifest.json
            .bengal/worktrees
            site/.bengal
          key: bengal-${{ runner.os }}-${{ steps.bengal-cache.outputs.hash }}
          # No restore-keys - we want exact match for correctness

      - name: Build site
        run: uv run bengal build --source . --all-versions --environment production --clean-output
        env:
          PYTHON_GIL: "0"

      - name: Prepare for upload
        run: |
          touch site/public/.nojekyll
          # Override parent .gitignore so upload-pages-artifact includes all files
          echo "" > site/public/.gitignore

      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v5
        with:
          path: site/public

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