name: Release Docs

on:
  release:
    types: [ released ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:


jobs:
  build-docs:
    permissions:
      contents: read
      id-token: write
    name: Build Docs
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version:
        - "3.11"
    steps:
    - uses: actions/checkout@v4
    - name: Install uv
      uses: astral-sh/setup-uv@v5
      with:
        enable-cache: true
        cache-dependency-glob: "uv.lock"
        python-version: ${{ matrix.python-version }}
    - name: Sync uv for docs
      run: uv sync --extra docs
    - name: Build docs
      run: |
        mkdir -p docs/build
        uv run pydoc-markdown .pydoc-markdown.yml

    - name: Checkout docs repo
      uses: actions/checkout@v4
      with:
        repository: aurelio-labs/docs
        path: temp-docs
        token: ${{ secrets.PAT }}

    - name: Copy and rename docs
      run: |
        ROOT_DIR=$(pwd)
        rm -rf temp-docs/semantic-router/client-reference
        mkdir -p temp-docs/semantic-router/client-reference/

        # Find and copy all md files
        cd docs/build/semantic_router
        find . -type f -name "*.md" | while read file; do
          dir=$(dirname "$file")
          echo "Processing file: $file in directory: $dir"
          mkdir -p "$ROOT_DIR/temp-docs/semantic-router/client-reference/$dir"
          cp "$file" "$ROOT_DIR/temp-docs/semantic-router/client-reference/${file%.md}.mdx"
        done

    - name: Update frontmatter keys
      run: |
        find temp-docs/semantic-router/client-reference -type f -name "*.mdx" -exec sed -i 's/sidebar_label:/sidebarTitle:/g' {} +

    - name: Install jq
      run: sudo apt-get install -y jq

    - name: Update mintlify docs.json navigation
      run: |
        # Get files and create grouped structure using jq
        files=$(cd temp-docs/semantic-router/client-reference && find . -type f -name "*.mdx" | sed 's|^./||' | sed 's|.mdx$||' | sort | jq -R -s '
          split("\n")[:-1] 
          | map("semantic-router/client-reference/" + .) 
          | reduce .[] as $path (
              {"root": [], "groups": {}};
              if ($path | split("/") | length) == 4 then
                .groups[$path | split("/")[2]] += [$path]
              else
                .root += [$path]
              end
            )
          | [.root[], (.groups | to_entries | map({
              "group": .key,
              "pages": .value
            }))[]
          ]
        ')

        # Update the docs.json file
        jq --arg pages "$files" '
          (.navigation.tabs[] | select(.tab == "Semantic Router") | 
           .groups[] | select(.group == "Client Reference") | 
           .pages) |= ($pages | fromjson)
        ' temp-docs/docs.json > temp-docs/docs.json.tmp && mv temp-docs/docs.json.tmp temp-docs/docs.json

    - name: Commit and push changes
      working-directory: temp-docs
      run: |
        git config --local user.email "github-actions[bot]@users.noreply.github.com"
        git config --local user.name "github-actions[bot]"
        git add .
        git diff --quiet && git diff --staged --quiet || (git commit -m "Update Semantic Router documentation" && git push)