name: Update AltStore Source

on:
  workflow_run:
    workflows: ["Build ALL"]
    types: [completed]
  workflow_dispatch:

jobs:
  update-source:
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.x'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install requests

    - name: Record job start time
      id: job_start_time
      run: echo "start_time=$(date +%s)" >> $GITHUB_OUTPUT

    - name: Update AltStore source
      id: update_source
      env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
      python update_alt_store.py
      git config --global user.name 'GitHub Action'
      git config --global user.email 'action@github.com'
      git add alt_store.json
      if git diff --staged --quiet; then
        echo "changes=false" >> $GITHUB_OUTPUT
      else
        # Create a new branch for the PR
        branch_name="update-altstore-$(date +%Y%m%d-%H%M%S)"
        git checkout -b "$branch_name"
        git commit -m "Updated source with latest release"
        git push -u origin "$branch_name"
        
        # Create PR using GitHub CLI
        gh pr create \
        --title "Update AltStore source with latest release" \
        --body "This PR updates the alt_store.json file with the latest release information." \
        --head "$branch_name" \
        --base main
        
        echo "changes=true" >> $GITHUB_OUTPUT
      fi

    - name: Calculate job duration
      id: duration
      if: always()
      run: |
        end_time=$(date +%s)
        duration=$((end_time - ${{ steps.job_start_time.outputs.start_time }}))
        echo "duration=$duration seconds" >> $GITHUB_OUTPUT

    - name: Create job summary
      run: |
        if [[ "${{ steps.update_source.outputs.changes }}" == "true" ]]; then
          echo "## Update Altstore Source Summary 🚀" >> $GITHUB_STEP_SUMMARY
          echo "✅ Changes Detected and Applied" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "The alt_store.json file has been updated with the latest release information." >> $GITHUB_STEP_SUMMARY
        else
          echo "## Update Altstore Source Summary 🚀" >> $GITHUB_STEP_SUMMARY
          echo "🔍 No Changes Detected" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "The alt_store.json file is up to date. No changes were necessary." >> $GITHUB_STEP_SUMMARY
        fi
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "🕐 Execution Time" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "This job took ${{ steps.duration.outputs.duration }} to complete." >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "📆 Next Scheduled Run" >> $GITHUB_STEP_SUMMARY
        echo "" >> $GITHUB_STEP_SUMMARY
        echo "The next scheduled run will be tomorrow at midnight UTC." >> $GITHUB_STEP_SUMMARY
