name: CI | Engine cyberia | github repository
on:
  workflow_dispatch:
    inputs:
      sync:
        description: 'Trigger sync-and-deploy CD after build'
        required: false
        default: 'false'
        type: choice
        options:
          - 'true'
          - 'false'
      message:
        description: 'Commit message for the build'
        required: false
        default: ''
      deploy_type:
        description: 'CD job type to dispatch (sync-and-deploy or init)'
        required: false
        default: 'sync-and-deploy'
        type: choice
        options:
          - sync-and-deploy
          - init
permissions:
  contents: write
  packages: write
  id-token: write
jobs:
  pwa-microservices-template:
    name: Update github repo package Jobs
    runs-on: ubuntu-latest
    container:
      image: quay.io/rockylinux/rockylinux:9
    permissions:
      contents: write
      packages: write
      id-token: write
    steps:
      - uses: actions/checkout@v7

      - name: Install required packages
        run: |
          dnf install -y sudo tar gzip bzip2 git
          dnf install -y curl --allowerasing

      - name: Install Node.js
        run: |
          curl -fsSL https://rpm.nodesource.com/setup_24.x | bash -
          dnf install nodejs -y

      - name: Install dependencies
        run: npm install

      - name: Set git global credentials
        run: |
          git config --global --add safe.directory '*'
          git config --global user.name 'underpostnet'
          git config --global user.email 'development@underpost.net'

      - name: Build repository
        env:
          GITHUB_TOKEN: ${{ secrets.GIT_AUTH_TOKEN }}
          GITHUB_USERNAME: ${{ github.repository_owner }}
        run: |
          cd .. && node engine/bin clone underpostnet/pwa-microservices-template
          if [ -n "${{ github.event.inputs.message }}" ]; then
            LAST_COMMIT_MESSAGE="${{ github.event.inputs.message }}"
          else
            LAST_COMMIT_MESSAGE=$(cd engine && node bin cmt --changelog-msg --from-n-commit 1 --changelog-no-hash 2>/dev/null || echo "")
          fi
          SHOULD_CD="${{ github.event.inputs.sync }}"
          DEPLOY_TYPE="${{ github.event.inputs.deploy_type }}"
          if [ -z "$LAST_COMMIT_MESSAGE" ]; then
            LAST_COMMIT_MESSAGE="Update engine cyberia repository"
          fi
          echo "[INFO] Captured commit message: $LAST_COMMIT_MESSAGE"
          echo "[INFO] Should CD: $SHOULD_CD, Deploy type: $DEPLOY_TYPE"
          node engine/bin clone --bare underpostnet/engine-cyberia
          cd engine
          git clone --depth 1 --no-checkout https://$GITHUB_TOKEN@github.com/underpostnet/engine-private.git ./engine-private
          cd ./engine-private && git sparse-checkout set conf/dd-cyberia && git checkout && cd ..
          node bin/build dd-cyberia development
          cd ../pwa-microservices-template
          rm -rf ./.git
          mv ../engine-cyberia.git ./.git
          mv ../engine/engine-private ./engine-private
          npm install
          node bin new --default-conf --deploy-id dd-cyberia
          mv ./conf.dd-cyberia.js ./conf.js
          rm -rf ./engine-private
          git init
          git config user.name 'underpostnet'
          git config user.email 'development@underpost.net'
          git add .
          if git diff --cached --quiet; then
            echo "[INFO] No changes to publish; skipping commit and push."
          else
            git commit -m "$LAST_COMMIT_MESSAGE"
            node ../engine/bin push . underpostnet/engine-cyberia
            echo "[INFO] Dispatching canonical github package build engine-ghpkg-cyberia"
            GHPKG_PAYLOAD=$(MSG="$LAST_COMMIT_MESSAGE" node -e '
              const i = { conf_id: "cyberia" };
              if (process.env.MSG) i.message = process.env.MSG;
              process.stdout.write(JSON.stringify({ ref: "master", inputs: i }));
            ')
            curl -s -X POST \
              -H "Accept: application/vnd.github.v3+json" \
              -H "Authorization: token ${{ secrets.GIT_AUTH_TOKEN }}" \
              "https://api.github.com/repos/underpostnet/engine/actions/workflows/ghpkg.ci.yml/dispatches" \
              -d "$GHPKG_PAYLOAD"
          fi
          if [ "$SHOULD_CD" = "true" ]; then
            if [ -z "$DEPLOY_TYPE" ]; then
              DEPLOY_TYPE="sync-and-deploy"
            fi
            echo "[INFO] Dispatching engine-cyberia CD ${DEPLOY_TYPE}"
            curl -s -X POST \
              -H "Accept: application/vnd.github.v3+json" \
              -H "Authorization: token ${{ secrets.GIT_AUTH_TOKEN }}" \
              "https://api.github.com/repos/underpostnet/engine/actions/workflows/engine-cyberia.cd.yml/dispatches" \
              -d "{\"ref\":\"master\",\"inputs\":{\"job\":\"${DEPLOY_TYPE}\"}}"
          else
            echo "[INFO] Deployment skipped"
          fi
