name: Claude Code Issue Handler

on:
  issue_comment:
    types: [created]

env:
  CLAUDE_MAX_TURNS: "30"

jobs:
  handle-issue:
    # Only run on issue comments, not PR comments
    if: ${{ !github.event.issue.pull_request }}
    runs-on: ubuntu-latest

    steps:
      - name: Classify issue by labels
        id: classify
        run: |
          LABELS='${{ toJson(github.event.issue.labels) }}'
          echo "Raw labels: $LABELS"
          IS_BUG=$(echo "$LABELS" | jq '[.[].name | ascii_downcase] | any(. == "bug")' 2>/dev/null || echo "false")
          IS_ENHANCEMENT=$(echo "$LABELS" | jq '[.[].name | ascii_downcase] | any(. == "enhancement")' 2>/dev/null || echo "false")
          echo "is_bug=$IS_BUG" >> $GITHUB_OUTPUT
          echo "is_enhancement=$IS_ENHANCEMENT" >> $GITHUB_OUTPUT
          echo "is_bug=$IS_BUG | is_enhancement=$IS_ENHANCEMENT"

      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITEA_TOKEN }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Install Claude Code CLI
        run: npm install -g @anthropic-ai/claude-code

      - name: Authenticate Claude Code CLI
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          claude config set apiKey "$ANTHROPIC_API_KEY"
          claude config set autoUpdaterStatus disabled

      - name: Configure Git
        run: |
          git config --global user.email "claude@vibentec-it.io"
          git config --global user.name "Claude Code Bot"
          git config --global url."https://oauth2:${{ secrets.GITEA_TOKEN }}@gitea.vibentec-it.io/".insteadOf "https://gitea.vibentec-it.io/"

      # ─────────────────────────────────────────────
      # BUG FLOW: research + fix + PR
      # ─────────────────────────────────────────────
      - name: "[Bug] Checkout fix branch"
        if: steps.classify.outputs.is_bug == 'true'
        run: |
          BRANCH_NAME="fix/issue-${{ github.event.issue.number }}"
          git checkout -b "$BRANCH_NAME"
          echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV

      - name: "[Bug] Run Claude Code to research and fix"
        if: steps.classify.outputs.is_bug == 'true'
        id: claude-bug
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          ISSUE_TITLE: ${{ github.event.issue.title }}
          ISSUE_BODY: ${{ github.event.issue.body }}
          ISSUE_COMMENT: ${{ github.event.comment.body }}
        run: |
          PROMPT=$(printf '%s\n' \
            "You are a software engineer fixing a bug reported in issue #${ISSUE_NUMBER}: \"${ISSUE_TITLE}\"." \
            "Issue description: ${ISSUE_BODY}" \
            "Comment that triggered this action: ${ISSUE_COMMENT}" \
            "Your tasks:" \
            "1. Read and understand the codebase relevant to this bug." \
            "2. Identify the root cause and which files need to change." \
            "3. Implement minimal, focused fixes — do not refactor unrelated code." \
            "4. After making code changes, run \"cd homepage && npm run lint\" via the Bash tool" \
            "   and immediately fix every lint error or syntax warning it reports." \
            "5. Do NOT run Jenkinsfile or any Jenkins-related checks." \
            "6. Do NOT run git commands yourself; the CI pipeline will commit your changes." \
            "After completing your investigation, fixes, and lint pass, output a brief plain-text" \
            "summary of the root cause, what you changed, and the lint result. Keep it under 200 words." \
          )
          claude \
            --allowedTools "Read,Write,Edit,Bash,Glob,Grep,WebSearch,WebFetch" \
            --max-turns "$CLAUDE_MAX_TURNS" \
            --output-format text \
            -p "$PROMPT" > /tmp/claude_bug_output.txt 2>&1 || true
          echo "--- Claude output ---"
          cat /tmp/claude_bug_output.txt

      - name: "[Bug] Check for file changes"
        if: steps.classify.outputs.is_bug == 'true'
        id: git-status
        run: |
          if git diff --quiet && git diff --cached --quiet; then
            echo "has_changes=false" >> $GITHUB_OUTPUT
            echo "No file changes detected."
          else
            echo "has_changes=true" >> $GITHUB_OUTPUT
            git diff --stat
          fi

      - name: "[Bug] Commit changes"
        if: steps.classify.outputs.is_bug == 'true' && steps.git-status.outputs.has_changes == 'true'
        run: |
          git add -A
          git commit -m "fix: resolve issue #${{ github.event.issue.number }} - ${{ github.event.issue.title }}"

      - name: "[Bug] Push branch"
        if: steps.classify.outputs.is_bug == 'true' && steps.git-status.outputs.has_changes == 'true'
        run: git push origin "$BRANCH_NAME"

      - name: "[Bug] Create Pull Request"
        if: steps.classify.outputs.is_bug == 'true' && steps.git-status.outputs.has_changes == 'true'
        id: create-pr
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          REPO: ${{ github.repository }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          ISSUE_TITLE: ${{ github.event.issue.title }}
          BASE_URL: ${{ github.server_url }}
        run: |
          CLAUDE_SUMMARY=$(cat /tmp/claude_bug_output.txt | tail -n 50)
          PR_TITLE="fix: resolve issue #${ISSUE_NUMBER} - ${ISSUE_TITLE}"
          PR_BODY=$(printf '## Summary\n\nThis PR addresses the bug reported in issue #%s.\n\n### Claude Code Analysis\n%s\n\n---\nCloses #%s' \
            "$ISSUE_NUMBER" "$CLAUDE_SUMMARY" "$ISSUE_NUMBER")
          PAYLOAD=$(jq -n \
            --arg title "$PR_TITLE" \
            --arg body "$PR_BODY" \
            --arg head "$BRANCH_NAME" \
            --arg base "main" \
            '{title: $title, body: $body, head: $head, base: $base}')
          PR_RESPONSE=$(curl -s -X POST \
            -H "Authorization: token $GITEA_TOKEN" \
            -H "Content-Type: application/json" \
            "${BASE_URL}/api/v1/repos/${REPO}/pulls" \
            -d "$PAYLOAD")
          PR_NUMBER=$(echo "$PR_RESPONSE" | jq -r '.number // empty')
          PR_URL=$(echo "$PR_RESPONSE" | jq -r '.html_url // empty')
          echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
          echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
          echo "Created PR #$PR_NUMBER: $PR_URL"

      - name: "[Bug] Comment on issue — PR created"
        if: >-
          steps.classify.outputs.is_bug == 'true' &&
          steps.git-status.outputs.has_changes == 'true' &&
          steps.create-pr.outputs.pr_url != ''
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          REPO: ${{ github.repository }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          BASE_URL: ${{ github.server_url }}
          PR_NUMBER: ${{ steps.create-pr.outputs.pr_number }}
          PR_URL: ${{ steps.create-pr.outputs.pr_url }}
        run: |
          COMMENT=$(printf "I've analyzed this bug and implemented a fix.\n\n**Pull Request:** [PR #%s](%s)\n\nPlease review the changes and merge when ready. If the fix doesn't fully address the issue, feel free to leave a comment and I'll take another look." \
            "$PR_NUMBER" "$PR_URL")
          PAYLOAD=$(jq -n --arg body "$COMMENT" '{body: $body}')
          curl -s -X POST \
            -H "Authorization: token $GITEA_TOKEN" \
            -H "Content-Type: application/json" \
            "${BASE_URL}/api/v1/repos/${REPO}/issues/${ISSUE_NUMBER}/comments" \
            -d "$PAYLOAD"

      - name: "[Bug] Comment on issue — no code changes needed"
        if: steps.classify.outputs.is_bug == 'true' && steps.git-status.outputs.has_changes == 'false'
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          REPO: ${{ github.repository }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          BASE_URL: ${{ github.server_url }}
        run: |
          ANALYSIS=$(cat /tmp/claude_bug_output.txt 2>/dev/null || echo "No analysis output available.")
          COMMENT=$(printf "After researching this issue, **no code changes appear to be necessary** at this time.\n\n### Analysis\n%s\n\nIf you believe something was missed, please provide more details and I'll investigate further." \
            "$ANALYSIS")
          PAYLOAD=$(jq -n --arg body "$COMMENT" '{body: $body}')
          curl -s -X POST \
            -H "Authorization: token $GITEA_TOKEN" \
            -H "Content-Type: application/json" \
            "${BASE_URL}/api/v1/repos/${REPO}/issues/${ISSUE_NUMBER}/comments" \
            -d "$PAYLOAD"

      # ─────────────────────────────────────────────
      # ENHANCEMENT FLOW: research + comment only
      # ─────────────────────────────────────────────
      - name: "[Enhancement] Run Claude Code to research"
        if: steps.classify.outputs.is_enhancement == 'true'
        id: claude-enhancement
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          ISSUE_TITLE: ${{ github.event.issue.title }}
          ISSUE_BODY: ${{ github.event.issue.body }}
          ISSUE_COMMENT: ${{ github.event.comment.body }}
        run: |
          PROMPT=$(printf '%s\n' \
            "You are a software architect reviewing a feature idea from issue #${ISSUE_NUMBER}: \"${ISSUE_TITLE}\"." \
            "Idea description: ${ISSUE_BODY}" \
            "Comment that triggered this action: ${ISSUE_COMMENT}" \
            "Your tasks:" \
            "1. Explore the existing codebase to understand what already exists related to this idea." \
            "2. Research best practices and possible implementation approaches." \
            "3. Identify technical challenges, risks, or dependencies." \
            "4. Provide a structured Markdown response suitable for posting as an issue comment." \
            "Format your response with these sections:" \
            "- **Current State** (what already exists)" \
            "- **Proposed Approach** (recommended implementation path)" \
            "- **Considerations** (risks, trade-offs, dependencies)" \
            "- **Next Steps** (actionable recommendations)" \
            "Keep the total response under 500 words." \
          )
          claude \
            --allowedTools "Read,Glob,Grep,WebSearch,WebFetch" \
            --max-turns 15 \
            --output-format text \
            -p "$PROMPT" > /tmp/claude_enhancement_output.txt 2>&1 || true
          echo "--- Claude output ---"
          cat /tmp/claude_enhancement_output.txt

      - name: "[Enhancement] Comment research findings on issue"
        if: steps.classify.outputs.is_enhancement == 'true'
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
          REPO: ${{ github.repository }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          BASE_URL: ${{ github.server_url }}
        run: |
          RESEARCH=$(cat /tmp/claude_enhancement_output.txt 2>/dev/null || echo "Research output unavailable.")
          COMMENT=$(printf "## Research Findings\n\nI've explored the codebase and researched this idea. Here's what I found:\n\n%s\n\n---\n*This research was performed automatically by Claude Code. Feel free to discuss further in the comments.*" \
            "$RESEARCH")
          PAYLOAD=$(jq -n --arg body "$COMMENT" '{body: $body}')
          curl -s -X POST \
            -H "Authorization: token $GITEA_TOKEN" \
            -H "Content-Type: application/json" \
            "${BASE_URL}/api/v1/repos/${REPO}/issues/${ISSUE_NUMBER}/comments" \
            -d "$PAYLOAD"
