name: Check and Update fullPage.js

on:
  schedule:
    # Run daily at 2 AM UTC
    - cron: '0 2 * * *'
  workflow_dispatch:
    # Allow manual trigger

jobs:
  check-fullpage-update:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0

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

      - name: Install dependencies
        run: npm ci

      - name: Get current fullPage.js version
        id: current-version
        run: |
          CURRENT_VERSION=$(node -p "require('./package.json').dependencies['fullpage.js']")
          echo "current_version=${CURRENT_VERSION}" >> $GITHUB_OUTPUT
          echo "Current fullPage.js version: $CURRENT_VERSION"

      - name: Get latest fullPage.js version from npm
        id: latest-version
        run: |
          LATEST_VERSION=$(npm view fullpage.js version)
          echo "latest_version=${LATEST_VERSION}" >> $GITHUB_OUTPUT
          echo "Latest fullPage.js version: $LATEST_VERSION"

      - name: Compare versions
        id: compare-versions
        run: |
          CURRENT_VERSION="${{ steps.current-version.outputs.current_version }}"
          LATEST_VERSION="${{ steps.latest-version.outputs.latest_version }}"
          
          # Remove the ^ from the current version for comparison
          CURRENT_CLEAN=$(echo $CURRENT_VERSION | sed 's/^[^0-9]*//')
          LATEST_CLEAN=$(echo $LATEST_VERSION | sed 's/^[^0-9]*//')
          
          echo "Current clean version: $CURRENT_CLEAN"
          echo "Latest clean version: $LATEST_CLEAN"
          
          if [ "$CURRENT_CLEAN" != "$LATEST_CLEAN" ]; then
            echo "update_needed=true" >> $GITHUB_OUTPUT
            echo "Update needed: $CURRENT_CLEAN -> $LATEST_CLEAN"
          else
            echo "update_needed=false" >> $GITHUB_OUTPUT
            echo "No update needed"
          fi

      - name: Update package.json if newer version available
        if: steps.compare-versions.outputs.update_needed == 'true'
        run: |
          LATEST_VERSION="${{ steps.latest-version.outputs.latest_version }}"
          npm install fullpage.js@$LATEST_VERSION --save
          echo "Updated fullPage.js to version $LATEST_VERSION"

      - name: Update package version
        if: steps.compare-versions.outputs.update_needed == 'true'
        run: |
          # Get current package version
          CURRENT_PKG_VERSION=$(node -p "require('./package.json').version")
          echo "Current package version: $CURRENT_PKG_VERSION"
          
          # Increment patch version
          IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_PKG_VERSION"
          NEW_PATCH=$((VERSION_PARTS[2] + 1))
          NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.$NEW_PATCH"
          
          # Update package.json version
          npm version $NEW_VERSION --no-git-tag-version
          echo "Updated package version to: $NEW_VERSION"

      - name: Build project
        if: steps.compare-versions.outputs.update_needed == 'true'
        run: |
          npm run build
          echo "Build completed successfully"

      - name: Push changes to dev branch
        if: steps.compare-versions.outputs.update_needed == 'true'
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          
          git add .
          git commit -m "chore: update fullPage.js to ${{ steps.latest-version.outputs.latest_version }}"
          git push origin dev
          echo "Changes committed and pushed to dev branch"

      - name: Send notification on success
        if: steps.compare-versions.outputs.update_needed == 'true'
        run: |
          echo "## ✅ Update Completed Successfully"
          echo "Updated fullPage.js from ${{ steps.current-version.outputs.current_version }} to ${{ steps.latest-version.outputs.latest_version }}"
          echo "New package version: $(node -p "require('./package.json').version")"
          echo "Changes pushed to dev branch"

      - name: Comment on issue/PR if no update needed
        if: steps.compare-versions.outputs.update_needed == 'false'
        run: |
          echo "No update needed. Current version (${{ steps.current-version.outputs.current_version }}) is up to date with latest (${{ steps.latest-version.outputs.latest_version }})." 