name: Style Reminders Comment

permissions:
  contents: read
  pull-requests: write

on:
  pull_request:
    branches:
      - v2
    types: [opened, synchronize, ready_for_review]

jobs:
  comment-style-reminders:
    runs-on: ubuntu-latest
    if: github.event.pull_request.draft == false && github.event.pull_request.head.repo.full_name == github.repository

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Install Node.js
        uses: actions/setup-node@v6
        with:
          node-version-file: '.node-version'

      - name: Install pnpm
        uses: pnpm/action-setup@v5

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_OUTPUT

      - name: Cache pnpm dependencies
        uses: actions/cache@v5
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-

      - name: Install dependencies
        run: pnpm install

      - name: Detect new style reminders
        run: pnpm exec tsx scripts/check-pr-style-reminders.ts --base "${{ github.event.pull_request.base.sha }}" --head "${{ github.event.pull_request.head.sha }}" --markdown-output .tmp/style-reminders-pr-comment.md

      - name: Upsert PR comment
        uses: actions/github-script@v8
        with:
          script: |
            const fs = require('node:fs');
            const path = require('node:path');

            const marker = '<!-- style-reminders-warning -->';
            const commentPath = path.join(process.env.GITHUB_WORKSPACE, '.tmp/style-reminders-pr-comment.md');
            const body = fs.existsSync(commentPath) ? fs.readFileSync(commentPath, 'utf8').trim() : '';

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

            const botComment = comments.find(comment =>
              comment.user.type === 'Bot' && comment.body.includes(marker)
            );

            if (!body) {
              if (botComment) {
                await github.rest.issues.deleteComment({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  comment_id: botComment.id,
                });
              }
              core.info('No new style reminders found in this PR.');
              return;
            }

            if (botComment) {
              await github.rest.issues.updateComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                comment_id: botComment.id,
                body,
              });
            } else {
              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.issue.number,
                body,
              });
            }
