name: Update .app-headers in /misc

on:
  push:
    branches: ["main"]
  workflow_dispatch:

jobs:
  update-and-merge-pr:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write

    steps:
      # Step 1: Setup job (Check environment & install dependencies)
      - name: Setup environment
        run: |
          echo "Setting up the environment for the workflow."
          sudo apt-get update
          sudo apt-get install -y figlet gh jq

      # Step 2: Generate Token
      - name: Generate a token
        id: generate-token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.CREATE_HEADER_APP_ID }}  
          private-key: ${{ secrets.CREATE_HEADER_SECRET }}

      # Step 3: Checkout repository
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Fetch all history to ensure we have full access to the repo history

      # Step 4: Check or Update Branch (Create or update the update-app-headers branch)
      - name: Check or Update Branch
        run: |
          git fetch origin
          if ! git show-ref --quiet refs/heads/update-app-headers; then
            echo "Creating the 'update-app-headers' branch as it does not exist."
            git checkout -b update-app-headers origin/main
          else
            echo "Switching to the existing 'update-app-headers' branch."
            git checkout update-app-headers
          fi

      # Step 5: Ensure .app-headers file exists (if missing, create it)
      - name: Ensure .app-headers file exists
        run: |
          if [ ! -f ".app-headers" ]; then
            echo "The .app-headers file does not exist. Creating it."
            touch .app-headers
            echo "Generated by CI" > .app-headers
          else
            echo ".app-headers already exists."
          fi

      # Step 6: Setup Figlet (Install and set up Figlet for text generation)
      - name: Set up Figlet
        run: sudo apt-get install -y figlet

      # Step 7: Run generate-app-headers script
      - name: Run generate-app-headers script
        run: |
          bash .github/workflows/generate-app-headers.sh

      # Step 8: Check if there are changes
      - name: Check for changes
        run: |
          git diff --exit-code || echo "Changes detected."

      # Step 9: Commit changes if any
      - name: Commit changes if any
        run: |
          git diff --exit-code || git commit -am "[core]: update .app-headers to latest version"

      # Step 10: Push changes to the branch
      - name: Push changes to the branch
        run: |
          git push origin update-app-headers --force || echo "No changes to push"

      # Step 11: Create Pull Request (If changes exist, create a PR)
      - name: Create PR
        id: create-pr
        if: steps.check-changes.outcome == 'success'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PR_EXISTS=$(gh pr list --head "update-app-headers" --json number --jq '.[].number')
          if [ -z "$PR_EXISTS" ]; then
            echo "Creating a new PR."
            gh pr create --title "[core]: update .app-headers to latest version" \
                         --body "This PR automatically updates the app-headers file." \
                         --head update-app-headers \
                         --base main
          else
            echo "PR already exists."
          fi

      # Step 12: Final status (Output status to console)
      - name: Output final status
        run: |
          echo "Workflow completed successfully. Branch and PR status updated."

      # Step 13: Post checkout repo (Make sure to clean up the repository state)
      - name: Post checkout repo
        run: |
          echo "Repository check complete."
          git status

      # Step 14: Post generate token (Output generated token for logging purposes)
      - name: Post generate token
        run: |
          echo "Generated token: ${GITHUB_TOKEN}"

      # Step 15: Complete (Final confirmation that workflow has finished)
      - name: Complete
        run: |