name: Forensics Check

on:
  issues:
    types: [opened, edited]

permissions:
  issues: write

jobs:
  check-forensics:
    # Only run on bug reports
    if: contains(github.event.issue.labels.*.name, 'bug')
    runs-on: blacksmith-4vcpu-ubuntu-2404
    steps:
      - name: Check for forensics output and comment if missing
        uses: actions/github-script@v7
        with:
          script: |
            const body = context.payload.issue.body || '';
            const issueNumber = context.payload.issue.number;
            const forensicsMarker = 'Auto-generated by `/sf forensics`';

            if (body.includes(forensicsMarker)) {
              core.info('Forensics output found in issue body — no comment needed.');
              return;
            }

            // Check comments too — reporter may have added it after opening
            const comments = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issueNumber,
            });

            const forensicsInComments = comments.data.some(c =>
              c.body && c.body.includes(forensicsMarker)
            );

            if (forensicsInComments) {
              core.info('Forensics output found in comments — no comment needed.');
              return;
            }

            // Avoid duplicate bot comments
            const botMarker = '<!-- sf-forensics-check -->';
            const alreadyCommented = comments.data.some(c =>
              c.user.type === 'Bot' && c.body && c.body.includes(botMarker)
            );

            if (alreadyCommented) {
              core.info('Forensics request comment already posted — skipping duplicate.');
              return;
            }

            const comment = [
              botMarker,
              '',
              'Thanks for the bug report! To help us investigate, please run `/sf forensics` in your project and paste the output here.',
              '',
              '```bash',
              '# In your project directory:',
              '/sf forensics',
              '```',
              '',
              'The forensics output includes git history analysis, session traces, stuck-loop detection, and cost data that significantly speeds up diagnosis.',
              '',
              '---',
              '*This is an automated check. If `/sf forensics` is not available in your version, you can skip this step.*',
            ].join('\n');

            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issueNumber,
              body: comment,
            });

            await github.rest.issues.addLabels({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: issueNumber,
              labels: ['needs-forensics'],
            });

            core.info('Posted forensics request comment.');
