name: Auto Release

on:
  push:
    branches:
      - main
    paths-ignore:
      - '**.md'
      - '.github/workflows/auto-release.yml'

permissions:
  contents: write

jobs:
  auto-release:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Setup Bun
        uses: oven-sh/setup-bun@v1
        with:
          bun-version: latest

      - name: Install system dependencies for skia-canvas
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            build-essential \
            libcairo2-dev \
            libpango1.0-dev \
            libjpeg-dev \
            libgif-dev \
            librsvg2-dev \
            libpixman-1-dev \
            pkg-config \
            python3 \
            make \
            g++ \
            libstdc++6

      - name: Install dependencies
        run: bun install

      - name: Rebuild native modules
        env:
          NODE_ENV: production
          SKIA_CANVAS_USE_SYSTEM_LIBRARIES: 1
        run: |
          cd node_modules/skia-canvas && npm rebuild
          cd ../..
          node -e "console.log('Testing skia-canvas import...'); require('skia-canvas'); console.log('✅ skia-canvas loaded successfully')"

      - name: Build action
        run: bun run build

      - name: Verify build
        run: |
          echo "Checking built files..."
          ls -la dist/
          echo "Testing action execution..."
          node -e "console.log('Testing action import...'); import('./dist/action.js').then(() => console.log('✅ Action loaded successfully')).catch(err => { console.error('❌ Action load failed:', err.message); process.exit(1); })"

      - name: Get commit history
        id: commits
        uses: actions/github-script@v7
        with:
          script: |
            const { execSync } = require('child_process');
            
            // Get commits since last tag
            const lastTag = execSync('git describe --tags --abbrev=0 2>/dev/null || echo ""', { encoding: 'utf8' }).trim();
            const commits = execSync(`git log ${lastTag ? lastTag + '..HEAD' : '--oneline'} --pretty=format:"%s"`, { encoding: 'utf8' }).trim().split('\n');
            
            console.log('Commits since last tag:', commits);
            
            return { commits: commits.join('\n') };

      - name: Determine version bump
        id: version
        run: |
          # Always bump patch version for auto-releases
          echo "bump_type=patch" >> $GITHUB_OUTPUT
          echo "Bump type: ${{ steps.version.outputs.bump_type }}"

      - name: Bump version
        id: bump
        run: |
          # Get current version
          CURRENT_VERSION=$(node -p "require('./package.json').version")
          echo "Current version: $CURRENT_VERSION"
          
          # Bump version
          NEW_VERSION=$(npm version ${{ steps.version.outputs.bump_type }} --no-git-tag-version)
          echo "New version: $NEW_VERSION"
          
          # Remove 'v' prefix if present
          NEW_VERSION=${NEW_VERSION#v}
          echo "Cleaned version: $NEW_VERSION"
          
          echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
          echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT

      - name: Commit version bump and built files
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add package.json dist/
          # Check if there are changes to commit
          if git diff --staged --quiet; then
            echo "No changes to commit, skipping commit"
          else
            git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }} and build action"
            git push
          fi

      - name: Check if release exists and handle tag/release
        id: release_check
        uses: actions/github-script@v7
        with:
          script: |
            const { execSync } = require('child_process');
            
            const tagName = 'v${{ steps.bump.outputs.new_version }}';
            const tagExists = execSync(`git tag -l "${tagName}"`, { encoding: 'utf8' }).trim() === tagName;
            
            console.log(`Tag ${tagName} exists: ${tagExists}`);
            
            if (tagExists) {
              // Check if release exists for this tag
              try {
                const release = await github.rest.repos.getReleaseByTag({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  tag: tagName
                });
                console.log(`Release for ${tagName} exists: true`);
                return { tagExists: true, releaseExists: true };
              } catch (error) {
                console.log(`Release for ${tagName} exists: false`);
                return { tagExists: true, releaseExists: false };
              }
            } else {
              console.log(`Tag ${tagName} does not exist`);
              return { tagExists: false, releaseExists: false };
            }

      - name: Bump version again if release exists
        if: steps.release_check.outputs.tagExists == 'true' && steps.release_check.outputs.releaseExists == 'true'
        id: rebump
        run: |
          echo "Release already exists for v${{ steps.bump.outputs.new_version }}, bumping version again"
          NEW_VERSION=$(npm version patch --no-git-tag-version)
          NEW_VERSION=${NEW_VERSION#v}
          echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
          echo "Bumped to: $NEW_VERSION"

      - name: Create and push tag
        run: |
          TAG_VERSION="${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}"
          echo "Creating tag: v$TAG_VERSION"
          
          # Check if tag already exists
          if git tag -l "v$TAG_VERSION" | grep -q "v$TAG_VERSION"; then
            echo "Tag v$TAG_VERSION already exists, skipping tag creation"
          else
            git tag v$TAG_VERSION
            git push origin v$TAG_VERSION
          fi

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
          release_name: Release v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
          body: |
            ## 🎉 Release v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
            
            ### Changes:
            ${{ steps.commits.outputs.commits }}
            
            ### Usage:
            Update your workflows to use:
            ```yaml
            uses: LukeHagar/usage-statistics@v${{ steps.release_check.outputs.releaseExists == 'true' && steps.rebump.outputs.new_version || steps.bump.outputs.new_version }}
            ```
          draft: false
          prerelease: false
        continue-on-error: true
