name: Notify

on:
  workflow_call:
    inputs:
      build-flavor:
        required: true
        type: string
      build-title:
        required: true
        type: string

    secrets:
      DISCORD_WEBHOOK_EDGE:
        required: true
      DISCORD_WEBHOOK_BETA:
        required: true
      DISCORD_WEBHOOK_CANDIDATE:
        required: true
      DISCORD_WEBHOOK_STABLE:
        required: true

jobs:
  discord:
    name: Discord
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Determine Webhook Channel
        id: webhook
        uses: actions/github-script@v6
        env:
          DISCORD_WEBHOOK_EDGE: ${{ secrets.DISCORD_WEBHOOK_EDGE }}
          DISCORD_WEBHOOK_BETA: ${{ secrets.DISCORD_WEBHOOK_BETA }}
          DISCORD_WEBHOOK_CANDIDATE: ${{ secrets.DISCORD_WEBHOOK_CANDIDATE }}
          DISCORD_WEBHOOK_STABLE: ${{ secrets.DISCORD_WEBHOOK_STABLE }}
          BUILD_FLAVOR: ${{ inputs.build-flavor }}
        with:
          result-encoding: string
          script: |
            switch (process.env.BUILD_FLAVOR) {
              case 'beta': return process.env.DISCORD_WEBHOOK_BETA;
              case 'candidate': return process.env.DISCORD_WEBHOOK_CANDIDATE;
              case 'stable': return process.env.DISCORD_WEBHOOK_STABLE;
              case 'edge':
              default: return process.env.DISCORD_WEBHOOK_EDGE;
            }

      - name: Create Discord Embed
        id: embed
        uses: actions/github-script@v6
        env:
          BUILD_FLAVOR: ${{ inputs.build-flavor }}
          BUILD_TITLE: ${{ inputs.build-title }}
        with:
          result-encoding: string
          script: return require('.github/scripts/notify-embed.js')();

      - name: Send Discord Message
        uses: Ilshidur/action-discord@0.3.2
        env:
          DISCORD_WEBHOOK: ${{ steps.webhook.outputs.result }}
          DISCORD_USERNAME: LunaSea Support
          DISCORD_AVATAR: https://raw.githubusercontent.com/JagandeepBrar/LunaSea/master/assets/icon/icon.png
          DISCORD_EMBEDS: ${{ steps.embed.outputs.result }}
