# codefidence — Memory Check for Pull Requests
# Auto-generated by `codefidence init --full`. Customize as needed.
#
# This workflow runs `codefidence check-diff` on each PR and posts a comment
# with relevant memory items when sensitivity is medium or high.
# Low-sensitivity PRs produce no comment (no noise).
#
# Prerequisites:
#   1. codefidence must be installed (cargo install codefidence)
#   2. A .wiki/ folder must exist in the repo
#   3. The merge=ours driver should be configured: git config merge.ours.driver true

name: Wiki Memory Check

on:
  pull_request:
    types: [opened, synchronize, reopened]

permissions:
  contents: read
  pull-requests: write

jobs:
  memory-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install codefidence
        run: cargo install codefidence --locked
        # Alternative: download a pre-built release for faster CI
        # - run: |
        #     curl -L https://github.com/OWNER/codefidence/releases/latest/download/codefidence-linux-x64 -o /usr/local/bin/codefidence
        #     chmod +x /usr/local/bin/codefidence

      - name: Get changed files
        id: files
        run: |
          FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD | tr '\n' ' ')
          echo "changed=$FILES" >> "$GITHUB_OUTPUT"

      - name: Run memory check
        id: check
        run: |
          # Pipe directly to file to avoid shell interpretation issues
          codefidence check-diff --pr-comment ${{ steps.files.outputs.changed }} > /tmp/wiki-comment.md 2>/dev/null || true
          if [ -s /tmp/wiki-comment.md ]; then
            echo "has_comment=true" >> "$GITHUB_OUTPUT"
          else
            echo "has_comment=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Post or update PR comment
        if: steps.check.outputs.has_comment == 'true'
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const body = fs.readFileSync('/tmp/wiki-comment.md', 'utf8');
            const marker = '<!-- codefidence-memory-check -->';

            // Find existing comment (idempotent)
            const { data: comments } = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
            });

            const existing = comments.find(c => c.body.includes(marker));

            if (existing) {
              await github.rest.issues.updateComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                comment_id: existing.id,
                body,
              });
              console.log('Updated existing wiki memory comment');
            } else {
              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.issue.number,
                body,
              });
              console.log('Created new wiki memory comment');
            }
