name: Version Bump

on:
  pull_request:
    types: [labeled]
    paths:
      - "registry/**/modules/**"

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

jobs:
  version-bump:
    if: github.event.label.name == 'version:patch' || github.event.label.name == 'version:minor' || github.event.label.name == 'version:major'
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write
      issues: write
    steps:
      - name: Checkout code
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Set up Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Set up Terraform
        uses: coder/coder/.github/actions/setup-tf@main

      - name: Install dependencies
        run: bun install

      - name: Extract bump type from label
        id: bump-type
        run: |
          case "${{ github.event.label.name }}" in
            "version:patch")
              echo "type=patch" >> $GITHUB_OUTPUT
              ;;
            "version:minor")
              echo "type=minor" >> $GITHUB_OUTPUT
              ;;
            "version:major")
              echo "type=major" >> $GITHUB_OUTPUT
              ;;
            *)
              echo "Invalid version label: ${{ github.event.label.name }}"
              exit 1
              ;;
          esac

      - name: Check version bump requirements
        id: version-check
        run: |
          output_file=$(mktemp)
          if ./.github/scripts/version-bump.sh "${{ steps.bump-type.outputs.type }}" origin/main > "$output_file" 2>&1; then
            echo "Script completed successfully"
          else
            echo "Script failed"
            cat "$output_file"
            exit 1
          fi

          {
            echo "output<<EOF"
            cat "$output_file"
            echo "EOF"
          } >> $GITHUB_OUTPUT

          cat "$output_file"

          if git diff --quiet; then
            echo "versions_up_to_date=true" >> $GITHUB_OUTPUT
            echo "✅ All module versions are already up to date"
          else
            echo "versions_up_to_date=false" >> $GITHUB_OUTPUT
            echo "❌ Module versions need to be updated"
            echo "Files that would be changed:"
            git diff --name-only
            echo ""
            echo "Diff preview:"
            git diff
            
            git checkout .
            git clean -fd
            
            exit 1
          fi

      - name: Comment on PR - Failure
        if: failure() && steps.version-check.outputs.versions_up_to_date == 'false'
        uses: actions/github-script@v8
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const output = `${{ steps.version-check.outputs.output }}`;
            const bumpType = `${{ steps.bump-type.outputs.type }}`;

            let comment = `## ❌ Version Bump Validation Failed\n\n`;
            comment += `**Bump Type:** \`${bumpType}\`\n\n`;
            comment += `Module versions need to be updated but haven't been bumped yet.\n\n`;
            comment += `**Required Actions:**\n`;
            comment += `1. Run the version bump script locally: \`./.github/scripts/version-bump.sh ${bumpType}\`\n`;
            comment += `2. Commit the changes: \`git add . && git commit -m "chore: bump module versions (${bumpType})"\`\n`;
            comment += `3. Push the changes: \`git push\`\n\n`;
            comment += `### Script Output:\n\`\`\`\n${output}\n\`\`\`\n\n`;
            comment += `> Please update the module versions and push the changes to continue.`;

            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: comment
            });
