name: Sync Upstream Firefox Releases

on:
  workflow_dispatch:
    inputs:
      release_candidate:
        description: "Set to true to sync release candidates"
        required: false
        type: boolean
        default: false
  workflow_call:
    inputs:
      release_candidate:
        description: "Set to true to sync release candidates"
        required: false
        type: boolean
        default: false

permissions:
  contents: write

jobs:
  check_candidates:
    name: Sync Upstream
    runs-on: ubuntu-latest

    steps:
      - name: Check out repository
        uses: actions/checkout@v3

      - name: Check if upstream branch already exists
        id: check-upstream-branch
        run: |
          if git ls-remote --heads origin chore/upstream-sync | grep -sw "refs/heads/chore/upstream-sync" > /dev/null; then
            echo "branch_exists=true" >> $GITHUB_OUTPUT
          else
            echo "branch_exists=false" >> $GITHUB_OUTPUT
          fi

      - name: Setup Node.js
        uses: actions/setup-node@v4
        if: steps.check-upstream-branch.outputs.branch_exists == 'false'
        with:
          node-version-file: ".nvmrc"

      - name: Install dependencies
        if: steps.check-upstream-branch.outputs.branch_exists == 'false'
        run: npm ci

      - name: Install dependencies
        if: steps.check-upstream-branch.outputs.branch_exists == 'false'
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

      - name: Setup surfer CI
        if: steps.check-upstream-branch.outputs.branch_exists == 'false'
        run: |
          if [ "${{ github.event.inputs.release_candidate }}" = "false" ]; then
            npm run surfer -- ci --brand release 
          fi

      - name: Restore Surfer engine cache
        id: surfer-engine-cache
        uses: actions/cache@v5
        with:
          path: .surfer/engine/
          key: surfer-engine-${{ hashFiles('surfer.json') }}

      - name: Download Firefox and dependencies
        if: steps.check-upstream-branch.outputs.branch_exists == 'false'
        run: npm run download

      - name: Sync Upstream Releases
        if: steps.check-upstream-branch.outputs.branch_exists == 'false'
        run: |
          if [ "${{ github.event.inputs.release_candidate }}" = "true" ]; then
            npm run sync:rc
          else
            npm run sync
          fi

      - name: Install requirements
        run: pip3 install -r requirements.txt

      - name: Check if any files changed
        id: git-check
        run: |
          if [ -n "$(git status --porcelain)" ]; then
            echo "files_changed=true" >> $GITHUB_OUTPUT
          else
            echo "files_changed=false" >> $GITHUB_OUTPUT
          fi

      - name: Get Firefox Version
        id: build-data
        if: steps.git-check.outputs.files_changed == 'true'
        run: |
          if [ "${{ github.event.inputs.release_candidate }}" = "true" ]; then
            VERSION=$(node -pe "require('./surfer.json').version.candidate")
          else
            VERSION=$(node -pe "require('./surfer.json').version.version")
          fi
          echo "version=$VERSION" >> $GITHUB_OUTPUT

      - name: Check if patches got applied
        if: steps.git-check.outputs.files_changed == 'true'
        id: check-patches
        continue-on-error: true
        run: |
          echo "Checking if patches apply cleanly..."
          npm run import

      - name: Import external tests
        if: steps.git-check.outputs.files_changed == 'true'
        run: python3 scripts/import_external_tests.py || true

      - name: Import external patches
        if: steps.git-check.outputs.files_changed == 'true'
        run: python3 scripts/update_external_patches.py || true

      - name: Create pull request
        uses: peter-evans/create-pull-request@v7
        if: steps.git-check.outputs.files_changed == 'true'
        env:
          GIT_TRACE: 1
          GIT_CURL_VERBOSE: 1
        with:
          token: ${{ secrets.DEPLOY_KEY }}
          commit-message: "chore: Sync upstream to `Firefox ${{ steps.build-data.outputs.version }}`"
          branch: "chore/upstream-sync"
          title: "Sync upstream Firefox to version ${{ steps.build-data.outputs.version }}"
          body: |
            This PR syncs the upstream Firefox to version ${{ steps.build-data.outputs.version }}.

            * ${{ steps.check-patches.outcome == 'failure' && '⚠️ Some patches did not apply cleanly. Please review them carefully.' || '✅ All patches applied cleanly.' }}

            @${{ github.actor }} please review and merge this PR.
          base: dev
          git-token: ${{ secrets.DEPLOY_KEY }}
          delete-branch: true
