name: Sync Engine Version

on:
  workflow_dispatch:

jobs:
  sync-engine:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write

    steps:
      - name: Checkout Repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          repository: flutteractionsbot/flutter
          token: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}
          ref: master
          persist-credentials: false

      - name: Configure git
        run: |
          gh auth setup-git
          git config --global user.name "flutteractionsbot"
          git config --global user.email "<flutter-actions-bot@google.com>"
        env:
          GH_TOKEN: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}

      - name: Fetch upstream
        run: |
          git remote add upstream https://github.com/flutter/flutter.git
          git fetch upstream "$REF_NAME"

          BRANCH_NAME="update-engine-$(date +%s)"
          echo "BRANCH_NAME=$BRANCH_NAME" >> "$GITHUB_ENV"

          git checkout -b "$BRANCH_NAME" "upstream/$REF_NAME"
        env:
          GH_TOKEN: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}
          REF_NAME: ${{ github.ref_name }}

      - name: Generate and Capture Hash
        id: generate-hash
        run: |
          # Capture the OLD hash (handle case where file doesn't exist)
          if [ -f bin/internal/engine.version ]; then
            OLD_HASH=$(< bin/internal/engine.version)
          else
            OLD_HASH="None (New File)"
          fi
          echo "OLD_HASH=$OLD_HASH" >> "$GITHUB_ENV"

          chmod +x bin/internal/last_engine_commit.sh
          NEW_HASH=$(./bin/internal/last_engine_commit.sh)

          echo "$NEW_HASH" > bin/internal/engine.version
          echo "ENGINE_HASH=$NEW_HASH" >> "$GITHUB_ENV"

      - name: Check for Changes
        id: git-check
        run: |
          # -f adds even if it's in the .gitignore (which it usually is)
          git add -f bin/internal/engine.version

          if git diff --cached --exit-code bin/internal/engine.version; then
            echo "changed=false" >> "$GITHUB_OUTPUT"

            # Nice big No-Op Summary
            echo "## ✅ Engine version already up to date" >> "$GITHUB_STEP_SUMMARY"
            echo "The engine is already at hash \`$ENGINE_HASH\`. No PR was created." >> "$GITHUB_STEP_SUMMARY"
          else
            echo "changed=true" >> "$GITHUB_OUTPUT"
          fi

      - name: Create PR if Changed
        if: steps.git-check.outputs.changed == 'true'
        run: |
          gh auth setup-git
          git commit -m "Update engine.version to $ENGINE_HASH"
          git push origin "$BRANCH_NAME"

          # Capture the PR URL
          PR_URL=$(gh pr create \
            --title "[$REF_NAME] Sync engine.version to $ENGINE_HASH" \
            --body "Updates the engine.version file from \`$OLD_HASH\` to \`$ENGINE_HASH\`." \
            --repo flutter/flutter \
            --base "$REF_NAME" \
            --head "flutteractionsbot:$BRANCH_NAME")

          # Nice big PR Success Summary
          {
            echo "## 🚀 PR Created Successfully"
            echo "Updated engine version from \`$OLD_HASH\` to \`$ENGINE_HASH\`."
            echo ""
            echo "🔗 **View Pull Request:** $PR_URL"
          } >> "$GITHUB_STEP_SUMMARY"
        env:
          GH_TOKEN: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}
          REF_NAME: ${{ github.ref_name }}