name: PR Greetings

on:
  pull_request:
    types: [opened]
  pull_request_target:
    types: [opened]

jobs:
  greeting:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Check if the PR creator is the repo owner or Weblate
      id: check_owner
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        PR_AUTHOR=$(jq -r '.pull_request.user.login' "$GITHUB_EVENT_PATH")
        REPO_OWNER=$(jq -r '.repository.owner.login' "$GITHUB_EVENT_PATH")
        
        if [ "$PR_AUTHOR" = "$REPO_OWNER" ] || [ "$PR_AUTHOR" = "weblate" ]; then
          echo "The PR creator is the repository owner or Weblate. Skipping greeting message."
          echo "skip=true" >> $GITHUB_ENV
        else
          echo "The PR creator is not the repository owner or Weblate. Checking for previous PRs..."
          
          # Check for both open and closed pull requests by the author
          OPEN_PRS=$(gh pr list --author "$PR_AUTHOR" --state open --json number --jq '. | length')
          CLOSED_PRS=$(gh pr list --author "$PR_AUTHOR" --state closed --json number --jq '. | length')
          TOTAL_PRS=$((OPEN_PRS + CLOSED_PRS))
          
          echo "User $PR_AUTHOR has created $TOTAL_PRS pull request(s) in total"
          echo "Open PRs: $OPEN_PRS"
          echo "Closed PRs: $CLOSED_PRS"
          
          if [ "$TOTAL_PRS" -eq 1 ]; then
            echo "This is the user's first pull request. Sending greeting message."
            echo "skip=false" >> $GITHUB_ENV
          else
            echo "User has previous pull requests. Skipping greeting message."
            echo "skip=true" >> $GITHUB_ENV
          fi
        fi

    - name: Send Greeting Message
      if: env.skip != 'true'
      uses: actions/github-script@v6
      with:
        script: |
          const prNumber = context.payload.pull_request.number;
          const message = `
          **🎉 Thank you for your contribution!**
          
          Your Pull Request has been successfully submitted and is now awaiting review. We truly appreciate your efforts to improve Dantotsu.

          **👥 Connect with the Community**
          
          While you're here, why not join our communities to stay engaged?
          - **[Discord](https://discord.com/invite/4HPZ5nAWwM)**: Chat with fellow developers, ask questions, and get the latest updates.
          - **[Telegram](https://t.me/dantotsuapp)**: Connect directly with us for real-time discussions and updates.

          **📋 What to Expect Next**
          - Our team will review your pull request as soon as possible.
          - You'll receive notifications if further information or changes are needed.
          - Once approved, your changes will be merged into the main project.

          We're excited to collaborate with you. Stay tuned for updates!
          `;
          
          await github.rest.issues.createComment({
            owner: context.repo.owner,
            repo: context.repo.repo,
            issue_number: prNumber,
            body: message
          });
