name: Issue Opened - Auto Label

on:
  issues:
    types: [opened]

jobs:
  auto-label:
    runs-on: ubuntu-latest
    permissions:
      issues: write
      contents: read

    steps:
      - name: Auto-label new issue
        uses: actions/github-script@v7
        with:
          script: |
            const title = context.payload.issue.title.toLowerCase();
            const body = (context.payload.issue.body || '').toLowerCase();
            const text = `${title} ${body}`;

            const labels = [];

            // Type labels
            if (text.includes('bug') || text.includes('error') || text.includes('fix')) {
              labels.push('🐛 type:bug');
            } else if (text.includes('feature') || text.includes('add') || text.includes('implement')) {
              labels.push('✨ type:feature');
            } else if (text.includes('doc') || text.includes('readme')) {
              labels.push('📚 type:docs');
            } else if (text.includes('refactor') || text.includes('cleanup')) {
              labels.push('♻️ type:refactor');
            } else if (text.includes('test') || text.includes('coverage')) {
              labels.push('🧪 type:test');
            }

            // Priority
            if (text.includes('urgent') || text.includes('critical')) {
              labels.push('📊 priority:P0-Critical');
            } else if (text.includes('important') || text.includes('high')) {
              labels.push('⚠️ priority:P1-High');
            } else {
              labels.push('📊 priority:P2-Medium');
            }

            // State
            labels.push('📥 state:pending');

            // Phase
            labels.push('🎯 phase:planning');

            if (labels.length > 0) {
              await github.rest.issues.addLabels({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.payload.issue.number,
                labels: labels
              });
            }
