name: Job messages ⚙️

on:
  workflow_call:
    inputs:
      branch:
        required: false
        type: string
      commit:
        required: true
        type: string
      preview_url:
        required: false
        type: string
      in_progress:
        required: true
        type: boolean
      comment:
        required: false
        type: boolean
      marker:
        description: Hidden marker to detect PR comments composed by the bot
        required: false
        type: string
        default: "CFPages-deployment"


jobs:
  cf_pages_msg:
    name: CloudFlare Pages deployment 📃🚀
    runs-on: ubuntu-latest

    steps:
      - name: Compose message 📃
        if: ${{ always() }}
        id: compose
        env:
          COMMIT: ${{ inputs.commit }}
          PREVIEW_URL: ${{ inputs.preview_url != '' && (inputs.branch != 'master' && inputs.preview_url || format('https://jellyfin-web.pages.dev ({0})', inputs.preview_url)) || 'Not available' }}
          DEPLOY_STATUS: ${{ inputs.in_progress && '🔄 Deploying...' || (inputs.preview_url != '' && '✅ Deployed!' || '❌ Failure. Check workflow logs for details') }}
          DEPLOYMENT_TYPE: ${{ inputs.branch != 'master' && '🔀 Preview' || '⚙️ Production' }}
          WORKFLOW_RUN: ${{ !inputs.in_progress && format('**[View build logs](https://github.com/{0}/actions/runs/{1})**', github.repository, github.run_id) || '' }}
        # EOF is needed for multiline environment variables in a GitHub Actions context
        run: |
          echo "## Cloudflare Pages deployment" > $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "| **Latest commit**       	| <code>${COMMIT::7}</code>     |" >> $GITHUB_STEP_SUMMARY
          echo "|-------------------------	|:----------------------------:	|" >> $GITHUB_STEP_SUMMARY
          echo "| **Status**              	| $DEPLOY_STATUS                |" >> $GITHUB_STEP_SUMMARY
          echo "| **Preview URL**         	| $PREVIEW_URL                  |" >> $GITHUB_STEP_SUMMARY
          echo "| **Type**         	        | $DEPLOYMENT_TYPE              |" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "$WORKFLOW_RUN" >> $GITHUB_STEP_SUMMARY
          COMPOSED_MSG=$(cat $GITHUB_STEP_SUMMARY)
          echo "msg<<EOF" >> $GITHUB_ENV
          echo "$COMPOSED_MSG" >> $GITHUB_ENV
          echo "EOF" >> $GITHUB_ENV

      - name: Push comment to Pull Request 🔼
        uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
        if: ${{ inputs.comment && steps.compose.conclusion == 'success' }}
        with:
          github-token: ${{ secrets.JF_BOT_TOKEN }}
          message: ${{ env.msg }}
          comment-tag: ${{ inputs.marker }}
