name: Issue Management Bot

on:
  issues:
    types: [opened, edited, labeled, unlabeled]

jobs:
  issue-triage:
    runs-on: ubuntu-latest
    env:
      API_TOKEN: ${{ secrets.BOT_ACCESS_TOKEN }}
      API_BASE_URL: https://git.psmattas.com/api/v1
      REPO: ${{ gitea.repository }}
      ISSUE_NUMBER: ${{ gitea.event.issue.number }}
      ISSUE_AUTHOR: ${{ gitea.event.issue.user.login }}
      ISSUE_TITLE: ${{ gitea.event.issue.title }}

    steps:
      - name: Welcome new contributors
        if: gitea.event.action == 'opened'
        run: |
          # Check if this is the user's first issue
          AUTHOR_ISSUES=$(curl -s -H "Authorization: token $API_TOKEN" "$API_BASE_URL/repos/$REPO/issues?state=all&created_by=$ISSUE_AUTHOR" | jq '. | length')

          if [ "$AUTHOR_ISSUES" -eq 1 ]; then
            WELCOME_MSG="👋 Welcome @$ISSUE_AUTHOR! Thanks for opening your first issue in Evercatch!\n\nOur team will review this shortly. In the meantime:\n- Check our [documentation](https://docs.evercatch.dev) for common solutions\n- Join our [community](https://community.evercatch.dev) for discussions\n- Review our [Code of Conduct](CODE_OF_CONDUCT.md)\n\nWe appreciate your contribution! 🎉"
            COMMENT_JSON=$(jq -n --arg body "$WELCOME_MSG" '{"body": $body}')
            curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$COMMENT_JSON" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/comments"
          fi

      - name: Auto-label bug reports
        if: contains(gitea.event.issue.title, '[BUG]') || contains(gitea.event.issue.labels.*.name, 'bug')
        run: |
          LABELS='{"labels": ["bug", "status: investigating"]}'
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$LABELS" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/labels"

      - name: Auto-label feature requests
        if: contains(gitea.event.issue.title, '[FEATURE]') || contains(gitea.event.issue.labels.*.name, 'feature')
        run: |
          LABELS='{"labels": ["feature", "status: under-review"]}'
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$LABELS" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/labels"

      - name: Auto-label support requests
        if: contains(gitea.event.issue.title, '[SUPPORT]') || contains(gitea.event.issue.labels.*.name, 'question')
        run: |
          LABELS='{"labels": ["question", "status: needs-response"]}'
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$LABELS" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/labels"

      - name: Auto-label documentation issues
        if: contains(gitea.event.issue.title, '[DOCS]') || contains(gitea.event.issue.labels.*.name, 'documentation')
        run: |
          LABELS='{"labels": ["documentation", "status: under-review"]}'
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$LABELS" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/labels"

      - name: Assign to team for critical bugs
        if: contains(gitea.event.issue.labels.*.name, 'priority: critical')
        run: |
          ASSIGNEES='{"assignees": ["psmattas"]}'
          curl -s -f -X PATCH -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$ASSIGNEES" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER"

          URGENT_MSG="🚨 **Critical Issue Detected**\n\nThis issue has been marked as critical and assigned to @psmattas for immediate attention.\n\nExpected response time: **4 hours**"
          COMMENT_JSON=$(jq -n --arg body "$URGENT_MSG" '{"body": $body}')
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$COMMENT_JSON" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/comments"

      - name: Request more info for incomplete issues
        if: gitea.event.action == 'opened' && !contains(gitea.event.issue.body, 'Steps to Reproduce') && contains(gitea.event.issue.labels.*.name, 'bug')
        run: |
          INFO_MSG="Hi @$ISSUE_AUTHOR! 👋\n\nTo help us investigate this bug, could you please provide:\n\n1. **Steps to reproduce** the issue\n2. **Expected behavior** vs **actual behavior**\n3. Your **subscription tier**\n4. **Event IDs** or **timestamps** (if applicable)\n\nThis will help us resolve the issue faster. Thanks!"
          COMMENT_JSON=$(jq -n --arg body "$INFO_MSG" '{"body": $body}')
          curl -s -f -X POST -H "Authorization: token $API_TOKEN" -H "Content-Type: application/json" -d "$COMMENT_JSON" "$API_BASE_URL/repos/$REPO/issues/$ISSUE_NUMBER/comments"

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