name: Create Release Commit

on:
  workflow_dispatch:
    inputs:
      tag:
        description: |
          Tag to release (aka 1.6.1a, 1.6.1-beta-3, 2.0, etc.).
          
          If this is just a release, then the version will be grabbed from the top line of version.txt. However, this field is needed if version.txt does not exist or is empty.
        type: string
        required: false
      release_type:
        description: 'Release Type. Will be ignored if not a release.'
        type: choice
        required: false
        default: 'Release'
        options:
          - 'Release'
          - 'Beta Release'
          - 'Alpha Release'
      update_files:
        description: |
          Whether this commit is just to update files. version.txt will not be changed. This is used when the templates are changed, and the main files need to be updated.
        type: boolean
        required: true
        default: false

  workflow_call:
    inputs:
      tag:
        description: |
          Tag to release (aka 1.6.1a, 1.6.1-beta-3, 2.0, etc.).
          
          If this is just a release, then the version will be grabbed from the top line of version.txt. However, this field is needed if version.txt does not exist or is empty.
        type: string
        required: false
      release_type:
        description: 'Release Type. Will be ignored if not a release.'
        type: string
        required: false
      update_files:
        description: |
          Whether this commit is just to update files. version.txt will not be changed. This is used when the templates are changed, and the main files need to be updated.
        type: boolean
        required: false
        default: false
        
jobs:
  createReleaseCommit:
    name: Create Release Commit (${{ inputs.version }})
    runs-on: ubuntu-latest
    env:
      UPDATE_FILES: ${{ inputs.update_files }}
      GITHUB_TAG: ${{ inputs.tag }}
      RELEASE_TYPE: ${{ inputs.release_type }}

    steps:
      - name: Get Token
        id: token
        uses: actions/create-github-app-token@v1
        with:
          app-id: ${{ secrets.APP_ID }}
          private-key: ${{ secrets.APP_KEY }}
          owner: Nomi-CEu

      - name: Checkout Repo
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ steps.token.outputs.token }}

      - name: Restore NPM Cached Files
        uses: actions/cache@v4
        id: npm-cache
        with:
          path: |
            ~/.npm
            ./tools/node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('./tools/package-lock.json') }}
          restore-keys: ${{ runner.os }}-npm-

      - name: Setup NodeJS v20
        uses: actions/setup-node@v4
        with:
          node-version: 20
          check-latest: true

      - name: Setup NPM Packages
        if: steps.npm-cache.outputs.cache-hit != 'true'
        working-directory: ./tools
        run: npm ci

      - name: Check Environmental Variables
        working-directory: ./tools
        run: npm run gulp check
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CURSEFORGE_PROJECT_ID: ${{ secrets.CURSEFORGE_PROJECT_ID }}
          CURSEFORGE_API_TOKEN: ${{ secrets.CURSEFORGE_API_TOKEN }}
          CFCORE_API_TOKEN: ${{ secrets.CFCORE_API_TOKEN }}

      - name: Update Files
        working-directory: ./tools
        run: npm run gulp updateFilesAll

      - name: Commit and Push Release Changes
        uses: stefanzweifel/git-auto-commit-action@v5
        id: commit-release
        if: ${{ !inputs.update_files }}
        with:
          commit_message: "${{ inputs.release_type }} ${{ inputs.tag }}\n\n[NO CATEGORY]"
          commit_author: nomi-ceu-management[bot] <155502507+nomi-ceu-management[bot]@users.noreply.github.com>
          commit_user_name: nomi-ceu-management[bot]
          commit_user_email: 155502507+nomi-ceu-management[bot]@users.noreply.github.com
          tagging_message: "${{ inputs.tag }}"

      - name: Commit and Push Update Changes
        uses: stefanzweifel/git-auto-commit-action@v5
        id: commit-update
        if: ${{ inputs.update_files }}
        with:
          commit_message: "Update Issue, Server and Version Config Files from Templates\n\n[SKIP]"
          commit_author: nomi-ceu-management[bot] <155502507+nomi-ceu-management[bot]@users.noreply.github.com>
          commit_user_name: nomi-ceu-management[bot]
          commit_user_email: 155502507+nomi-ceu-management[bot]@users.noreply.github.com

      - name: Throw Error if No Changes were Detected
        if: ${{ steps.commit-release.outputs.changes_detected == 'false' || steps.commit-update.outputs.changes_detected == 'false' }}
        run: |
          echo "No Changes were Detected. Most likely, this is an error."
          exit 1
