name: Update Component Registry (Direct Push)

on:
  push:
    branches:
      - main
    paths:
      - '.opencode/**'
      - '!registry.json'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  update-and-validate-registry:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y jq
      
      - name: Make scripts executable
        run: |
          chmod +x scripts/registry/validate-registry.sh
          chmod +x scripts/registry/auto-detect-components.sh
          chmod +x scripts/registry/register-component.sh
      
      - name: Auto-detect new components
        id: auto_detect
        run: |
          echo "## 🔍 Auto-Detection Results" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          
          # Run auto-detect in dry-run mode first
          if ./scripts/registry/auto-detect-components.sh --dry-run > /tmp/detect-output.txt 2>&1; then
            cat /tmp/detect-output.txt >> $GITHUB_STEP_SUMMARY
            
            # Check if new components were found
            if grep -q "Found.*new component" /tmp/detect-output.txt; then
              echo "new_components=true" >> $GITHUB_OUTPUT
              echo "" >> $GITHUB_STEP_SUMMARY
              echo "⚠️ New components detected - will auto-add to registry" >> $GITHUB_STEP_SUMMARY
            else
              echo "new_components=false" >> $GITHUB_OUTPUT
              echo "✅ No new components found" >> $GITHUB_STEP_SUMMARY
            fi
          else
            echo "new_components=false" >> $GITHUB_OUTPUT
            echo "❌ Auto-detection failed" >> $GITHUB_STEP_SUMMARY
          fi
      
      - name: Add new components to registry
        if: steps.auto_detect.outputs.new_components == 'true'
        run: |
          echo "## 📝 Adding New Components" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          
          ./scripts/registry/auto-detect-components.sh --auto-add | tee -a $GITHUB_STEP_SUMMARY
      
      - name: Validate registry
        id: validate
        run: |
          echo "## ✅ Registry Validation" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          
          if ./scripts/registry/validate-registry.sh -v > /tmp/validation-output.txt 2>&1; then
            echo "validation=passed" >> $GITHUB_OUTPUT
            echo "✅ All registry paths are valid!" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
            tail -20 /tmp/validation-output.txt >> $GITHUB_STEP_SUMMARY
            echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
          else
            echo "validation=failed" >> $GITHUB_OUTPUT
            echo "❌ Registry validation failed!" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
            cat /tmp/validation-output.txt >> $GITHUB_STEP_SUMMARY
            echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "⚠️ WARNING: Direct push to main with invalid registry!" >> $GITHUB_STEP_SUMMARY
            echo "Please fix registry.json and push a correction." >> $GITHUB_STEP_SUMMARY
            # Don't exit 1 here - we already pushed to main, just warn
          fi
      
      - name: Commit registry updates
        if: steps.auto_detect.outputs.new_components == 'true'
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          
          if ! git diff --quiet registry.json; then
            git add registry.json
            git commit -m "chore: auto-update registry with new components [skip ci]"
            git push
            
            echo "## 🚀 Registry Updated" >> $GITHUB_STEP_SUMMARY
            echo "" >> $GITHUB_STEP_SUMMARY
            echo "Registry has been automatically updated with new components." >> $GITHUB_STEP_SUMMARY
          fi
      
      - name: Summary
        if: always()
        run: |
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "---" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "### Registry Statistics" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          jq -r '
            "- **Agents:** \(.components.agents | length)",
            "- **Subagents:** \(.components.subagents | length)",
            "- **Commands:** \(.components.commands | length)",
            "- **Tools:** \(.components.tools | length)",
            "- **Plugins:** \(.components.plugins | length)",
            "- **Contexts:** \(.components.contexts | length)"
          ' registry.json >> $GITHUB_STEP_SUMMARY
