#!/bin/bash

# Comprehensive GitHub Issue Labels Setup Script
# Creates standardized labels for the agentic workflow template project
# Combines both AI workflow and general project labels
# Usage: ./setup-labels.sh

set -e

echo "🏷️  Setting up GitHub issue labels..."

# Disable exit on error for label creation (individual failures shouldn't stop the script)
set +e

# Function to create or update a label
create_or_update_label() {
  local name="$1"
  local color="$2"
  local description="$3"

  if gh label create "$name" --color "$color" --description "$description" --force 2>/dev/null; then
    echo "✓ Created/updated label: $name"
  else
    echo "⚠ Failed to create label: $name (may already exist with different settings)"
    # Try to update existing label instead
    if gh label edit "$name" --color "$color" --description "$description" 2>/dev/null; then
      echo "✓ Updated existing label: $name"
    else
      echo "⚠ Could not update label: $name"
    fi
  fi
}

# AI Workflow Labels (essential for workflow functionality)
echo "Creating AI workflow labels..."
create_or_update_label "ai-workflow" "7057FF" "AI workflow development and improvements"
create_or_update_label "ai-task" "C5DEF5" "Task to be processed by AI workflow"
create_or_update_label "ai-completed" "28A745" "AI task completed and PR created"
create_or_update_label "ai-no-changes" "FEF2C0" "AI determined no changes were needed"
create_or_update_label "ai-generated" "7057FF" "PR was generated by AI automation"
create_or_update_label "ai-reviewed" "9F4F96" "Reviewed by AI PR review system"
create_or_update_label "ai-review-needed" "FFA500" "Manual trigger to request AI review of PR"
create_or_update_label "ai-review-failed" "D93F0B" "AI review failed - manual review needed"

# AI Fix Labels (automatically added by quality check workflows)
echo "Creating AI fix labels..."
create_or_update_label "ai-fix-lint" "E99695" "Automatically added when lint checks fail"
create_or_update_label "ai-fix-tests" "E99695" "Automatically added when tests fail"
create_or_update_label "ai-fix-security" "E99695" "Automatically added when security scans fail"

# Priority Labels
echo "Creating priority labels..."
create_or_update_label "critical" "B60205" "Must be fixed immediately"
create_or_update_label "high" "D93F0B" "Should be fixed soon"
create_or_update_label "medium" "FBCA04" "Normal priority"
create_or_update_label "low" "0E8A16" "Nice to have"

# Type Labels
echo "Creating type labels..."
create_or_update_label "bug" "D73A4A" "Something isn't working"
create_or_update_label "enhancement" "A2EEEF" "New feature or request"
create_or_update_label "documentation" "0075CA" "Improvements or additions to documentation"
create_or_update_label "security" "B60205" "Security-related issues"
create_or_update_label "performance" "D4C5F9" "Performance improvements"
create_or_update_label "refactoring" "00FF00" "Code refactoring"
create_or_update_label "testing" "F9D0C4" "Testing improvements"

# Scope Labels
echo "Creating scope labels..."
create_or_update_label "phase-1" "1D76DB" "Phase 1 roadmap items"
create_or_update_label "phase-2" "0366D6" "Phase 2 roadmap items"
create_or_update_label "workflow" "E4E669" "GitHub Actions workflow"
create_or_update_label "infrastructure" "5319E7" "Infrastructure setup"

# Status Labels
echo "Creating status labels..."
create_or_update_label "ready" "0E8A16" "Ready for development"
create_or_update_label "in-progress" "FBCA04" "Currently being worked on"
create_or_update_label "blocked" "D93F0B" "Cannot proceed"
create_or_update_label "needs-info" "D876E3" "More information needed"
create_or_update_label "needs-review" "0366D6" "Needs code review"
create_or_update_label "wontfix" "CFD3D7" "Will not be implemented"

# Special Labels
echo "Creating special labels..."
create_or_update_label "epic" "84B6EB" "A collection of user stories and tasks that make up a larger feature."
create_or_update_label "feature-request" "a2eeef" "A new feature or enhancement."
create_or_update_label "technical-spec" "006b75" "A technical specification"
create_or_update_label "user-story" "006b75" "A user story"
create_or_update_label "breaking-change" "B60205" "Breaking API changes"
create_or_update_label "good first issue" "7057FF" "Good for newcomers"
create_or_update_label "help wanted" "008672" "Extra attention needed"
create_or_update_label "duplicate" "CFD3D7" "Duplicate of another issue"
create_or_update_label "security-review-needed" "E99695" "Requires security review"
create_or_update_label "security-review-completed" "0E8A16" "Security review completed"
create_or_update_label "performance-impact" "F9D71C" "May impact performance"
create_or_update_label "needs-docs" "BFD4F2" "Documentation updates needed"

# Release Labels
echo "Creating release labels..."
create_or_update_label "release" "00FF00" "Ready for release - triggers npm publishing when PR merged to main"

# Re-enable exit on error for final commands
set -e

echo ""
echo "✅ Label setup complete!"
echo ""
echo "📋 Available labels:"
gh label list

echo ""
echo "🔗 Next steps:"
echo "1. Apply labels to existing issues"
echo "2. Update issue templates to include labeling guidance"
echo "3. Document labeling guidelines for contributors"
