name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  release:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      contents: write
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      
      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest
      
      - name: Cache Bun dependencies
        uses: actions/cache@v4
        with:
          path: ~/.bun/install/cache
          key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }}
          restore-keys: |
            ${{ runner.os }}-bun-
      
      - name: Install dependencies
        run: bun install
      
      - name: Extract version from tag
        id: get_version
        run: |
          VERSION=${GITHUB_REF#refs/tags/v}
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Extracted version: $VERSION"
      
      - name: Verify manifest version matches tag
        run: |
          MANIFEST_VERSION=$(jq -r '.version' manifest.json)
          TAG_VERSION=${{ steps.get_version.outputs.version }}
          if [ "$MANIFEST_VERSION" != "$TAG_VERSION" ]; then
            echo "Error: manifest.json version ($MANIFEST_VERSION) does not match tag version ($TAG_VERSION)"
            exit 1
          fi
          echo "✓ manifest.json version matches tag: $TAG_VERSION"
      
      - name: Build plugin
        run: bun run build
      
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          name: ${{ steps.get_version.outputs.version }}
          prerelease: true
          token: ${{ secrets.GITHUB_TOKEN }}
          files: |
            manifest.json
            main.js
            styles.css
