# Tells Nomi-CEu-Translations to Update English Lang
name: "Update English Lang in Nomi-CEu-Translations"

on:
  workflow_dispatch:
  push:
    branches:
      # Only Update English Lang on Branch Main
      - main
    paths:
      # Specify the path to 'assets' folder, suffixed by '/**/[eE][nN]_[uU][sS].lang', then another one, suffixed by '/**/[eE][nN]-[uU][sS].lang' (Can't use character group [-_])
      # This also activates on QB Json Change, as if that happens, the QB Lang most likely needs to be changed.
      - "overrides/resources/**/[eE][nN]_[uU][sS].lang"
      - "overrides/resources/**/[eE][nN]-[uU][sS].lang"
      - "overrides/config/betterquesting/DefaultQuests.json" # Normal QB Json in Default Config
      - "overrides/config/betterquesting/saved_quests/ExpertQuests.json" # Expert QB Json in Default Config
      - "overrides/config-overrides/normal/betterquesting/DefaultQuests.json" # Normal QB Json in Config Overrides
      - "overrides/config-overrides/expert/betterquesting/DefaultQuests.json" # Expert QB Json in Config Overrides

jobs:
  updateLang:
    name: Update English Lang in Nomi-CEu-Translations
    runs-on: ubuntu-latest
    # Prevent Workflow from Running on Forks
    if: "${{ github.repository_owner == 'Nomi-CEu' }}"
    steps:
      # We have to use a custom app token, as the default GITHUB_TOKEN only has access to the base repo.
      # Nomi-CEu-Management has access to all of Nomi-CEu's Repos.
      - name: Get Token
        id: token
        uses: actions/create-github-app-token@v1
        with:
          # Shared Org Secret: Contains the Nomi-CEu-Management App ID (773030)
          app-id: ${{ secrets.APP_ID }}
          # Shared Org Secret: Contains the Nomi-CEu-Management App's Private Key.
          # run `cat {PEM_FILE_PATH} | base64 -w 0 && echo` to encode the key first if changing the key.
          # Paste the output of the command into the secret value.
          private-key: ${{ secrets.APP_KEY }}
          owner: Nomi-CEu
          
      - name: Dispatch Workflow
        uses: actions/github-script@v5
        with:
          github-token: ${{ steps.token.outputs.token }}
          # A Javascript Function Body to send the Workflow Dispatch Event.
          script: |
            await github.rest.repos.createDispatchEvent({
              owner: "Nomi-CEu", 
              repo: "Nomi-CEu-Translations",
              event_type: "update_english_lang",
              client_payload: {
                module: "nomi-ceu",
              },
            });
