name: Issue Triage
on:
  issues:
    types: [opened, reopened]

permissions:
  actions: read
  contents: read
  issues: write
  id-token: write

concurrency:
  group: issue-triage-${{ github.event.issue.number }}
  cancel-in-progress: true

jobs:
  triage:
    if: github.event.issue.pull_request == null
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Run Claude issue triager
        uses: anthropics/claude-code-action@v1
        with:
          anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
          github_token: ${{ secrets.GITHUB_TOKEN }}
          claude_args: |
            --model sonnet
            --max-turns 12
            --allowedTools "Bash(gh:*)" "WebSearch" "WebFetch"
          prompt: |
            You are the issue triage agent for this repository.

            Issue context:
            - Number: #${{ github.event.issue.number }}
            - Title: ${{ github.event.issue.title }}
            - URL: ${{ github.event.issue.html_url }}
            - Trigger: ${{ github.event.action }}
            - Current labels:
              ${{ toJson(github.event.issue.labels) }}
            - Body:
              ${{ github.event.issue.body }}

            Triage responsibilities:
            1. Ensure title prefix:
            - Every issue title must start with a type prefix. If the prefix is missing or wrong, fix the title. Preserve the rest of the title as-is.
            - Use Title Case for the description after the prefix (e.g., "Bug: Playback Stalls on iOS Safari").
            - Prefixes:
              - `Feature:` — New capability or behavior
              - `Bug:` — Something broken or not working as expected
              - `Docs:` — Writing or improving documentation content (not tooling)
              - `Architecture:` — Internal structure, design patterns, core refactoring
              - `Chore:` — Maintenance, deps, tooling, CI
              - `Design:` — Design docs, component specs, discovery/research
            2. Classify and label:
            - Determine best-fit labels from issue content and template sections.
            - Review labels used on related/cross-referenced issues before deciding.
            - Add missing useful labels and remove clearly incorrect labels.
            - Do not create new labels; only use existing labels.
            - Never apply these labels — they are managed manually or by other automation:
              - `epic` (epic linkage is curated by maintainers)
              - Priority labels (`P0`, `P1`, `P2`)
              - `triage` (applied by automation only for external authors)
              Surface these as recommendations in step 8 instead.
            3. Detect probable duplicates:
            - Search for existing issues with the same bug/request symptoms, stack traces, repro steps, or scope.
            - If a likely duplicate is found, leave one concise comment linking the likely canonical issue and why it appears duplicated.
            - If confidence is not high, do not mark as duplicate; instead leave a concise comment flagging the uncertainty for human review.
            - Do not auto-close duplicates unless there is very high confidence and no meaningful unique information.
            4. Escalate uncertainty:
            - When classification is ambiguous, conflicting, or needs human judgment, leave a concise comment flagging the uncertainty rather than applying labels.
            5. Suggest docs/resources when helpful:
            - Use https://videojs.org/llms.txt as the primary source to discover relevant docs links.
            - If likely already documented, suggest 1-3 relevant docs links with a short reason.
            - Prioritize repository/site docs over random sources.
            6. Ask for missing critical info only when needed:
            - If reproduction or environment details are insufficient, post a short targeted follow-up.
            7. Help users unblock when issue is about usage:
            - If the issue suggests the user is struggling to achieve something with the library, provide practical guidance.
            - Suggest the most relevant documented approach with 1-3 links (from https://videojs.org/llms.txt discovery).
            - Include concise next steps or a minimal example/snippet when it materially helps.
            - If docs appear incomplete or unclear, call that out in a comment for human follow-up.
            8. Planning recommendations (comment-only):
            - Review roadmap context at https://github.com/orgs/videojs/projects/7 and current repository milestones.
            - Identify likely related epics/issues and related open PRs.
            - Recommend whether this issue should be added to the roadmap.
            - Recommend a milestone if one is a strong fit.
            - Recommend related epic linkage if one clearly fits.
            - Recommend obvious `blocked by` relationships when clearly implied.
            - Recommend a priority level (P0–P2) when clearly implied by severity or urgency.
            - Recommend other existing issues that should be included in project planning alongside this one when relevant (for example, skin integration, docs API reference coverage, or other adjacent planning gaps).
            - Do not directly add the issue to roadmap/projects, milestones, epics, blocked-by links, or priority labels.
            - Instead, leave one concise comment with recommendations only when meaningful.
            9. Avoid noise:
            - Do not comment if there is no meaningful action or new information.
            - Keep comments concise and action-oriented.
