name: Win Store File on Release
on:
  #  release:
  #    types: [published]
  push:
    branches: [test/git-actions]
    tags:
      - v*
  workflow_dispatch:
    inputs: {}

jobs:
  windows-store-artifact:
    runs-on: windows-latest
    env:
      UNSPLASH_KEY: ${{ secrets.UNSPLASH_KEY }}
      UNSPLASH_CLIENT_ID: ${{ secrets.UNSPLASH_CLIENT_ID }}

    if: '!github.event.release.prerelease'

    steps:
      - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
        with:
          node-version: 22
      # required because setting via env.TZ does not work on windows
      - name: Set timezone to Europe Standard Time
        uses: szenius/set-timezone@1f9716b0f7120e344f0c62bb7b1ee98819aefd42 # v2.0
        with:
          timezoneWindows: 'W. Europe Standard Time'

      - name: Check out Git repository
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
        with:
          persist-credentials: false
      # work around for npm installs from git+https://github.com/johannesjo/J2M.git
      - name: Reconfigure git to use HTTP authentication
        run: >
          git config --global url."https://github.com/".insteadOf
          ssh://git@github.com/

      - name: Load Electron Builder Windows Store Config
        run: echo $WIN_STORE_ELECTRON_BUILDER_YML | base64 --decode > electron-builder.yaml
        shell: bash
        env:
          WIN_STORE_ELECTRON_BUILDER_YML: ${{secrets.WIN_STORE_ELECTRON_BUILDER_YML}}

      - name: Get npm cache directory
        id: npm-cache-dir
        shell: bash
        run: |
          echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT
      - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v5
        id: npm-cache # use this to check for `cache-hit` ==> if: steps.npm-cache.outputs.cache-hit != 'true'
        with:
          path: ${{ steps.npm-cache-dir.outputs.dir }}
          key: ${{ runner.os }}-node22-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node22-

      - name: Workaround for nx issue
        run: npm install @nx/nx-win32-x64-msvc

      - name: Install npm Packages
        #  if: steps.npm-cache.outputs.cache-hit != 'true'
        run: npm i

      - run: npm run env # Generate env.generated.ts

      - name: Build Frontend & Electron
        run: npm run build

      - name: Build/Release Electron app
        uses: johannesjo/action-electron-builder@9ea9e2d991c97668843d57337848e3e2b1ffab3d # v1
        with:
          build_script_name: empty
          release: false
          github_token: ${{ secrets.github_token }}

      - name: 'Upload Artifact'
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: WinStoreRelease
          path: .tmp/app-builds/*.appx

      # Pull the GitHub release body for this tag so the maintainer can paste it
      # into Partner Center's "What's new" field during the manual appx upload.
      # The release may not exist yet when this job runs (parallel release workflow);
      # in that case we leave a clear pointer and don't fail the build.
      - name: Collect release notes for Partner Center upload
        if: startsWith(github.ref, 'refs/tags/v')
        shell: bash
        env:
          GH_TOKEN: ${{ secrets.github_token }}
        run: |
          tag="${GITHUB_REF_NAME}"
          mkdir -p .tmp
          notes_file=".tmp/RELEASE_NOTES.md"

          if gh release view "$tag" --repo "${GITHUB_REPOSITORY}" --json body -q .body > "$notes_file" 2>/dev/null && [ -s "$notes_file" ]; then
            echo "Found release notes for $tag ($(wc -c < "$notes_file") bytes)"
          else
            printf 'No GitHub release body for %s yet. Check %s/releases/tag/%s once it exists.\n' \
              "$tag" "https://github.com/$GITHUB_REPOSITORY" "$tag" > "$notes_file"
          fi

          {
            echo "## What's new for $tag - paste into Partner Center"
            echo
            echo "Partner Center's 'What's new' field accepts plain text up to ~1500 chars."
            echo "Trim Markdown if needed."
            echo
            echo '```'
            cat "$notes_file"
            echo '```'
          } >> "$GITHUB_STEP_SUMMARY"

      - name: Upload release notes alongside appx
        if: startsWith(github.ref, 'refs/tags/v')
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: WinStoreReleaseNotes
          path: .tmp/RELEASE_NOTES.md
#      - name: Upload to Microsoft Store
#        if: startsWith(github.ref, 'refs/tags/v')
#        uses: isaacrlevin/windows-store-action@1.0
#        with:
#          tenant-id: ${{ secrets.AZURE_AD_TENANT_ID }}
#          client-id: ${{ secrets.AZURE_AD_CLIENT_ID }}
#          client-secret: ${{ secrets.AZURE_AD_CLIENT_SECRET }}
#          app-id: ${{ secrets.MICROSOFT_STORE_APP_ID }}
#          package-path: '.tmp/app-builds/*.appx'
#          skip-polling: false
