name: Replace PR Plugin with Staging Redirect on Merge

# This workflow runs when a PR is merged and replaces the PR-specific plugin 
# with a redirect version that points to the main staging URL.
# This ensures users who installed the PR version will automatically 
# update to the staging version on their next update check.

on:
  pull_request:
    types:
      - closed
  workflow_dispatch:
    inputs:
      pr_number:
        description: "PR number to test with"
        required: true
        type: string
      pr_merged:
        description: "Simulate merged PR"
        required: true
        type: boolean
        default: true

jobs:
  push-staging-redirect:
    if: (github.event_name == 'pull_request' && github.event.pull_request.merged == true) || (github.event_name == 'workflow_dispatch' && inputs.pr_merged == true)
    runs-on: ubuntu-latest
    permissions:
      contents: read
      actions: read
    steps:
      - name: Set PR number
        id: pr_number
        run: |
          if [ "${{ github.event_name }}" == "pull_request" ]; then
            echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
          else
            echo "pr_number=${{ inputs.pr_number }}" >> $GITHUB_OUTPUT
          fi

      - name: Download artifact
        uses: dawidd6/action-download-artifact@v11
        with:
          name_is_regexp: true
          name: unraid-plugin-.*
          path: connect-files
          pr: ${{ steps.pr_number.outputs.pr_number }}
          workflow: main.yml
          workflow_conclusion: success
          search_artifacts: true
          if_no_artifact_found: fail

      - name: Update Downloaded Plugin to Redirect to Staging
        run: |
          # Find the .plg file in the downloaded artifact
          plgfile=$(find connect-files -name "*.plg" -type f | head -1)
          if [ ! -f "$plgfile" ]; then
            echo "ERROR: .plg file not found in connect-files/"
            ls -la connect-files/
            exit 1
          fi

          echo "Found plugin file: $plgfile"
          
          # Get current version and bump it with current timestamp
          current_version=$(grep '<!ENTITY version' "${plgfile}" | sed -E 's/.*"(.*)".*/\1/')
          echo "Current version: ${current_version}"
          
          # Create new version with current timestamp (ensures it's newer)
          new_version=$(date +"%Y.%m.%d.%H%M")
          echo "New redirect version: ${new_version}"
          
          # Update version to trigger update
          sed -i -E "s#(<!ENTITY version \").*(\">)#\1${new_version}\2#g" "${plgfile}" || exit 1
                
          # Change the plugin url to point to staging - users will switch to staging on next update
          url="https://preview.dl.unraid.net/unraid-api/dynamix.unraid.net.plg"
          sed -i -E "s#(<!ENTITY plugin_url \").*?(\">)#\1${url}\2#g" "${plgfile}" || exit 1
          
          echo "Modified plugin to redirect to: ${url}"
          echo "Version bumped from ${current_version} to ${new_version}"
          
          mkdir -p pr-release
          mv "${plgfile}" pr-release/dynamix.unraid.net.plg

      - name: Clean up old PR artifacts from Cloudflare
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.CF_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: auto
        run: |
          # Delete all existing files in the PR directory first (txz, plg, etc.)
          aws s3 rm s3://${{ secrets.CF_BUCKET_PREVIEW }}/unraid-api/tag/PR${{ steps.pr_number.outputs.pr_number }}/ \
            --recursive \
            --endpoint-url ${{ secrets.CF_ENDPOINT }}
          
          echo "✅ Cleaned up old PR artifacts"

      - name: Upload PR Redirect Plugin to Cloudflare
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.CF_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: auto
        run: |
          # Upload only the redirect plugin file
          aws s3 cp pr-release/dynamix.unraid.net.plg \
            s3://${{ secrets.CF_BUCKET_PREVIEW }}/unraid-api/tag/PR${{ steps.pr_number.outputs.pr_number }}/dynamix.unraid.net.plg \
            --endpoint-url ${{ secrets.CF_ENDPOINT }} \
            --content-encoding none \
            --acl public-read
          
          echo "✅ Uploaded redirect plugin"

      - name: Output redirect information
        run: |
          echo "✅ PR plugin replaced with staging redirect version"
          echo "PR URL remains: https://preview.dl.unraid.net/unraid-api/tag/PR${{ steps.pr_number.outputs.pr_number }}/dynamix.unraid.net.plg"
          echo "Redirects users to staging: https://preview.dl.unraid.net/unraid-api/dynamix.unraid.net.plg"
          echo "Users updating from this PR version will automatically switch to staging"

      - name: Comment on PR about staging redirect
        if: github.event_name == 'pull_request'
        uses: thollander/actions-comment-pull-request@v3
        with:
          comment-tag: pr-closed-staging
          mode: recreate
          message: |
            ## 🔄 PR Merged - Plugin Redirected to Staging
            
            This PR has been merged and the preview plugin has been updated to redirect to the staging version.
            
            **For users testing this PR:**
            - Your plugin will automatically update to the staging version on the next update check
            - The staging version includes all merged changes from this PR
            - No manual intervention required
            
            **Staging URL:**
            ```
            https://preview.dl.unraid.net/unraid-api/dynamix.unraid.net.plg
            ```
            
            Thank you for testing! 🚀
