name: PR Management Bot

on:
  pull_request:
    types: [opened, edited, synchronize, reopened]
    branches: [main]

jobs:
  pr-bot:
    runs-on: ubuntu-latest
    env:
      API_TOKEN: ${{ secrets.BOT_ACCESS_TOKEN }}
      API_BASE_URL: https://git.psmattas.com/api/v1
      REPO: ${{ gitea.repository }}
      PR_NUMBER: ${{ gitea.event.pull_request.number }}
      PR_AUTHOR: ${{ gitea.actor }}

    steps:
      - name: Check out repository
        uses: https://gitea.com/actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Auto-label from commit messages
        run: |
          API_URL="$API_BASE_URL/repos/$REPO/pulls/$PR_NUMBER/commits"
          MESSAGES=$(curl -s -H "Authorization: token $API_TOKEN" "$API_URL" | jq -r '.[].commit.message')
          UNIQUE_TYPES=$(echo "$MESSAGES" | grep -oP '^EC-\d+: \K(FEAT|FIX|DOCS|STYLE|REFACTOR|PERF|TEST|CHORE)' | sort -u)
          if [[ -z "$UNIQUE_TYPES" ]]; then exit 0; fi
          LABELS_JSON=$(echo "$UNIQUE_TYPES" | jq -R '("type: " + (. | ascii_downcase))' | jq -s '{"labels": .}')
          ISSUES_API_URL="$API_BASE_URL/repos/$REPO/issues/$PR_NUMBER/labels"
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$LABELS_JSON" "$ISSUES_API_URL"

      - name: Auto-assign author
        if: gitea.event.pull_request.assignees_count == 0 && gitea.actor != 'gitea-actions' && gitea.actor != 'EC-bot'
        run: |
          ISSUES_EDIT_API_URL="$API_BASE_URL/repos/$REPO/issues/$PR_NUMBER"
          JSON_PAYLOAD=$(echo "$PR_AUTHOR" | jq -R '{"assignees": [.]}')
          curl -s -f -X PATCH -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$JSON_PAYLOAD" "$ISSUES_EDIT_API_URL"

      - name: Add conflict label and comment
        if: gitea.event.pull_request.merge_status == 'CONFLICT'
        run: |
          COMMENT_BODY="🚨 **Merge Conflict Detected** 🚨\n\nHi @$PR_AUTHOR, this pull request has merge conflicts with the \`main\` branch. Please resolve them by rebasing or merging \`main\` into your branch so the PR can be reviewed."
          COMMENT_JSON=$(jq -n --arg body "$COMMENT_BODY" '{"body": $body}')
          ISSUES_API_URL="$API_BASE_URL/repos/$REPO/issues/$PR_NUMBER/comments"
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$COMMENT_JSON" "$ISSUES_API_URL"

          LABEL_JSON='{ "labels": ["status: needs-rebase"] }'
          LABELS_API_URL="$API_BASE_URL/repos/$REPO/issues/$PR_NUMBER/labels"
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$LABEL_JSON" "$LABELS_API_URL"

      - name: Remove 'needs-rebase' label if clean
        if: gitea.event.pull_request.merge_status != 'CONFLICT'
        run: |
          LABEL_TO_REMOVE="status: needs-rebase"
          ISSUES_API_URL="$API_BASE_URL/repos/$REPO/issues/$PR_NUMBER"

          CURRENT_LABELS_JSON=$(curl -s -H "Authorization: token $API_TOKEN" "$ISSUES_API_URL/labels")
          LABEL_ID=$(echo "$CURRENT_LABELS_JSON" | jq --arg name "$LABEL_TO_REMOVE" '.[] | select(.name == $name) | .id')

          if [ -n "$LABEL_ID" ]; then
            echo "Found and removing '$LABEL_TO_REMOVE' (ID: $LABEL_ID)..."
            curl -s -f -X DELETE -H "Authorization: token $API_TOKEN" "$ISSUES_API_URL/labels/$LABEL_ID"
          fi

      - name: Auto-assign reviewers
        run: |
          REVIEWERS_JSON='{ "reviewers": ["psmattas"] }'

          if [[ -n "$REVIEWERS_JSON" ]]; then
            echo "Requesting review from: $REVIEWERS_JSON"
            API_URL="$API_BASE_URL/repos/$REPO/pulls/$PR_NUMBER/requested_reviewers"
            curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$REVIEWERS_JSON" "$API_URL"
          fi
