name: Auto Update fullPage.js

on:
  pull_request:
    types: [opened, synchronize, closed]
    paths:
      - 'package.json'
      - 'package-lock.json'
    branches:
      - main

jobs:
  check-fullpage-update:
    runs-on: ubuntu-latest
    outputs:
      is-fullpage-update: ${{ steps.check-update.outputs.is-fullpage-update }}
      new-version: ${{ steps.check-update.outputs.new-version }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 2  # Need at least 2 commits to compare

      - name: Check if fullPage.js was updated
        id: check-update
        run: |
          # Only run if PR is merged (closed)
          if [ "${{ github.event.action }}" != "closed" ]; then
            echo "is-fullpage-update=false" >> $GITHUB_OUTPUT
            echo "PR not merged yet, skipping"
            exit 0
          fi
          
          # Check if package.json was modified
          if git diff HEAD~1 --name-only | grep -q "package.json"; then
            # Check if fullpage.js version changed
            OLD_VERSION=$(git show HEAD~1:package.json | grep '"fullpage.js"' | sed 's/.*"fullpage.js": "\([^"]*\)".*/\1/')
            NEW_VERSION=$(grep '"fullpage.js"' package.json | sed 's/.*"fullpage.js": "\([^"]*\)".*/\1/')
            
            echo "Old version: $OLD_VERSION"
            echo "New version: $NEW_VERSION"
            
            if [ "$OLD_VERSION" != "$NEW_VERSION" ]; then
              echo "is-fullpage-update=true" >> $GITHUB_OUTPUT
              echo "new-version=$NEW_VERSION" >> $GITHUB_OUTPUT
              echo "fullPage.js updated from $OLD_VERSION to $NEW_VERSION"
            else
              echo "is-fullpage-update=false" >> $GITHUB_OUTPUT
              echo "No fullPage.js version change detected"
            fi
          else
            echo "is-fullpage-update=false" >> $GITHUB_OUTPUT
            echo "No package.json changes detected"
          fi

  build-and-test:
    needs: check-fullpage-update
    if: needs.check-fullpage-update.outputs.is-fullpage-update == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

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

      - name: Install dependencies
        run: npm ci

      - name: Build project
        run: npm run build

      - name: Run tests
        run: npm run test:jest

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist-files
          path: dist/

  create-release:
    needs: [check-fullpage-update, build-and-test]
    if: needs.check-fullpage-update.outputs.is-fullpage-update == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist-files
          path: dist/

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'
          cache: 'npm'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm ci

      - name: Bump version
        run: |
          # Get current version
          CURRENT_VERSION=$(node -p "require('./package.json').version")
          
          # Extract patch version and increment
          PATCH_VERSION=$(echo $CURRENT_VERSION | cut -d. -f3)
          NEW_PATCH=$((PATCH_VERSION + 1))
          NEW_VERSION=$(echo $CURRENT_VERSION | sed "s/\.[0-9]*$/.$NEW_PATCH/")
          
          # Update package.json version
          npm version $NEW_VERSION --no-git-tag-version
          
          echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV

      - name: Commit and push changes
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add package.json dist/
          git commit -m "chore: update fullPage.js to ${{ needs.check-fullpage-update.outputs.new-version }} and bump version to ${{ env.NEW_VERSION }}"
          git push

      - name: Create Release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: v${{ env.NEW_VERSION }}
          release_name: Release v${{ env.NEW_VERSION }}
          body: |
            ## What's Changed
            
            - Updated fullPage.js to version ${{ needs.check-fullpage-update.outputs.new-version }}
            - Rebuilt distribution files with latest fullPage.js
            
            ## Installation
            
            ```bash
            npm install @fullpage/react-fullpage@${{ env.NEW_VERSION }}
            ```
          draft: false
          prerelease: false

      - name: Publish to npm
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 