name: Create Project Labels

# This workflow only runs manually to set up labels
on:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}
  cancel-in-progress: false

jobs:
  create-labels:
    runs-on: ubuntu-latest
    permissions:
      issues: write
    steps:
      - name: Create all project labels
        uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
        with:
          script: |
            const labels = [
              // Priority labels
              { name: 'critical', color: 'B60205', description: 'Must have for the release, blocks other work' },
              { name: 'high', color: 'D93F0B', description: 'Important feature, should be included' },
              { name: 'medium', color: 'FBCA04', description: 'Nice to have, can be deferred' },
              { name: 'low', color: '0E8A16', description: 'Future enhancement, not urgent' },

              // Milestone labels
              { name: 'alpha', color: '5319E7', description: 'Part of initial alpha release' },
              { name: 'beta', color: '0052CC', description: 'Part of beta release' },
              { name: 'post-beta', color: '006B75', description: 'Post-beta enhancement' },

              // Category labels
              { name: 'architecture', color: 'C5DEF5', description: 'System design and structure' },
              { name: 'backend', color: '1D76DB', description: 'Server-side code' },
              { name: 'frontend', color: '5EBEFF', description: 'UI/UX code' },
              { name: 'feature', color: 'A2EEEF', description: 'New functionality' },
              { name: 'security', color: 'EE0701', description: 'Security-related' },
              { name: 'ssl', color: 'F9D0C4', description: 'SSL/TLS certificates' },
              { name: 'sso', color: 'D4C5F9', description: 'Single Sign-On' },
              { name: 'waf', color: 'B60205', description: 'Web Application Firewall' },
              { name: 'crowdsec', color: 'FF6B6B', description: 'CrowdSec integration' },
              { name: 'caddy', color: '1F6FEB', description: 'Caddy-specific' },
              { name: 'database', color: '006B75', description: 'Database-related' },
              { name: 'ui', color: '7057FF', description: 'User interface' },
              { name: 'deployment', color: '0E8A16', description: 'Docker, installation' },
              { name: 'monitoring', color: 'FEF2C0', description: 'Logging and statistics' },
              { name: 'documentation', color: '0075CA', description: 'Docs and guides' },
              { name: 'testing', color: 'BFD4F2', description: 'Test suite' },
              { name: 'performance', color: 'EDEDED', description: 'Optimization' },
              { name: 'community', color: 'D876E3', description: 'Community building' },
              { name: 'plus', color: 'FFD700', description: 'Premium/"Plus" feature' },
              { name: 'enterprise', color: '8B4513', description: 'Enterprise-grade feature' }
            ];

            for (const label of labels) {
              try {
                await github.rest.issues.createLabel({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  name: label.name,
                  color: label.color,
                  description: label.description
                });
                console.log(`✓ Created label: ${label.name}`);
              } catch (error) {
                if (error.status === 422) {
                  console.log(`⚠ Label already exists: ${label.name}`);
                  // Update the label if it exists
                  await github.rest.issues.updateLabel({
                    owner: context.repo.owner,
                    repo: context.repo.repo,
                    name: label.name,
                    color: label.color,
                    description: label.description
                  });
                  console.log(`✓ Updated label: ${label.name}`);
                } else {
                  console.error(`✗ Error creating label ${label.name}:`, error.message);
                }
              }
            }
