name: Publish Chrome Extension

on:
  push:
    tags:
      - 'v*'

jobs:
  publish:
    name: Pack and Upload Extension
    runs-on: ubuntu-latest

    env:
      EXTENSION_ID: ${{ secrets.GOOGLE_EXTENSION_ID }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Install dependencies
        run: npm ci

      - name: Build extension
        run: npm run build

      - name: Set version from tag
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT

      - name: Update manifest version
        run: node scripts/update-manifest-version.js ${{ steps.version.outputs.VERSION }}

      - name: Create zip package
        run: |
          zip -r goose-highlighter.zip . -x '*.git*' 'node_modules/*' 'src/*' 'scripts/*' 'versioning.md' '.releaserc.json' 'package.json' 'package-lock.json' 'README.md' 'tsconfig.json' 'eslint.config.mjs'

      - name: Install webstore upload CLI
        run: npm install -g chrome-webstore-upload-cli

      - name: Upload to Chrome Web Store
        run: |
          chrome-webstore-upload upload \
            --source goose-highlighter.zip \
            --extension-id $EXTENSION_ID \
            --client-id ${{ secrets.GOOGLE_CLIENT_ID }} \
            --client-secret ${{ secrets.GOOGLE_CLIENT_SECRET }} \
            --refresh-token ${{ secrets.GOOGLE_REFRESH_TOKEN }}

      - name: Publish extension
        run: |
          chrome-webstore-upload publish \
            --extension-id $EXTENSION_ID \
            --client-id ${{ secrets.GOOGLE_CLIENT_ID }} \
            --client-secret ${{ secrets.GOOGLE_CLIENT_SECRET }} \
            --refresh-token ${{ secrets.GOOGLE_REFRESH_TOKEN }}

