name: Generate Release Notes from CodeRabbit summary

on:
  issue_comment:
    types: [created]

jobs:
  generate-release-notes:
    # Only run on PR comments from CodeRabbit bot
    if: github.event.issue.pull_request && github.event.comment.user.login == 'coderabbitai[bot]'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: write
      pull-requests: write
      issues: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
      - name: Set up environment
        uses: ./.github/actions/setup
        with:
          download-translations: 'false'

      - name: Check if this is CodeRabbit's first comment
        id: check-first-comment
        run: node .github/actions/ai-generated-release-notes/check-first-comment.js
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_EVENT_ISSUE_NUMBER: ${{ github.event.issue.number }}
          GITHUB_EVENT_COMMENT_ID: ${{ github.event.comment.id }}

      - name: Get PR details
        if: steps.check-first-comment.outputs.result == 'true'
        id: pr-details
        run: node .github/actions/ai-generated-release-notes/pr-details.js
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_EVENT_ISSUE_NUMBER: ${{ github.event.issue.number }}

      - name: Check if PR targets master branch
        if: steps.check-first-comment.outputs.result == 'true' && steps.pr-details.outputs.result != 'null'
        id: check-base-branch
        run: |
          BASE_BRANCH=$(echo '${{ steps.pr-details.outputs.result }}' | jq -r '.baseBranch')
          echo "Base branch: $BASE_BRANCH"
          if [ "$BASE_BRANCH" = "master" ]; then
            echo "targets_master=true" >> $GITHUB_OUTPUT
          else
            echo "targets_master=false" >> $GITHUB_OUTPUT
            echo "PR does not target master branch, skipping release notes generation"
          fi

      - name: Check if release notes file already exists
        if: steps.check-first-comment.outputs.result == 'true' && steps.pr-details.outputs.result != 'null' && steps.check-base-branch.outputs.targets_master == 'true'
        id: check-release-notes-exists
        run: node .github/actions/ai-generated-release-notes/check-release-notes-exists.js
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_EVENT_ISSUE_NUMBER: ${{ github.event.issue.number }}
          PR_DETAILS: ${{ steps.pr-details.outputs.result }}

      - name: Generate summary with OpenAI
        if: steps.check-first-comment.outputs.result == 'true' && steps.check-release-notes-exists.outputs.result == 'false'
        id: generate-summary
        run: node .github/actions/ai-generated-release-notes/generate-summary.js
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          GITHUB_EVENT_COMMENT_BODY: ${{ github.event.comment.body }}
          PR_DETAILS: ${{ steps.pr-details.outputs.result }}

      - name: Determine category with OpenAI
        if: steps.check-first-comment.outputs.result == 'true' && steps.check-release-notes-exists.outputs.result == 'false' && steps.generate-summary.outputs.result != 'null'
        id: determine-category
        run: node .github/actions/ai-generated-release-notes/determine-category.js
        env:
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          GITHUB_EVENT_COMMENT_BODY: ${{ github.event.comment.body }}
          PR_DETAILS: ${{ steps.pr-details.outputs.result }}
          SUMMARY_DATA: ${{ steps.generate-summary.outputs.result }}

      - name: Create and commit release notes file via GitHub API
        if: steps.check-first-comment.outputs.result == 'true' && steps.check-release-notes-exists.outputs.result == 'false' && steps.generate-summary.outputs.result != 'null' && steps.determine-category.outputs.result != 'null' && steps.determine-category.outputs.result != ''
        run: node .github/actions/ai-generated-release-notes/create-release-notes-file.js
        env:
          GITHUB_TOKEN: ${{ secrets.ACTIONS_UPDATE_TOKEN }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_EVENT_ISSUE_NUMBER: ${{ github.event.issue.number }}
          SUMMARY_DATA: ${{ steps.generate-summary.outputs.result }}
          CATEGORY: ${{ steps.determine-category.outputs.result }}

      - name: Comment on PR
        if: steps.check-first-comment.outputs.result == 'true' && steps.check-release-notes-exists.outputs.result == 'false' && steps.generate-summary.outputs.result != 'null' && steps.determine-category.outputs.result != 'null' && steps.determine-category.outputs.result != ''
        run: node .github/actions/ai-generated-release-notes/comment-on-pr.js
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_EVENT_ISSUE_NUMBER: ${{ github.event.issue.number }}
          SUMMARY_DATA: ${{ steps.generate-summary.outputs.result }}
          CATEGORY: ${{ steps.determine-category.outputs.result }}
