name: Generate WIDOCO Docs

on:
  push:
    branches: [main]
    paths:
      - "specs/v*/celine.ttl"

permissions:
  contents: write

jobs:
  widoco:
    name: Generate WIDOCO HTML Docs
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Detect changed spec version
        id: detect
        run: |
          version=$(git diff --name-only HEAD~1 HEAD \
            | grep -oP 'specs/\K(v[^/]+)(?=/celine\.ttl)' \
            | tail -n 1)

          if [ -z "$version" ]; then
            echo "No celine.ttl change detected — skipping"
            echo "skip=true" >> "$GITHUB_OUTPUT"
          else
            echo "version=$version" >> "$GITHUB_OUTPUT"
            echo "skip=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Generate WIDOCO docs
        if: steps.detect.outputs.skip != 'true'
        run: |
          version="${{ steps.detect.outputs.version }}"
          mkdir -p "releases/$version"
          chmod 777 "releases/$version"

          docker run --rm \
            -v "${{ github.workspace }}/specs/$version:/usr/local/widoco/in:ro" \
            -v "${{ github.workspace }}/releases/$version:/usr/local/widoco/out" \
            ghcr.io/dgarijo/widoco:v1.4.23 \
            -ontFile /usr/local/widoco/in/celine.ttl \
            -outFolder /usr/local/widoco/out \
            -rewriteAll \
            -lang en \
            -includeAnnotationProperties

      - name: Update releases/current symlink
        if: steps.detect.outputs.skip != 'true'
        run: |
          version="${{ steps.detect.outputs.version }}"
          ln -sfn "$version" releases/current

      - name: Commit and push docs
        if: steps.detect.outputs.skip != 'true'
        run: |
          version="${{ steps.detect.outputs.version }}"
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add "releases/$version" releases/current
          git diff --cached --quiet || git commit -m "docs: generate WIDOCO docs for $version"
          git push
