name: Publish to snap store on release
on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      tag_name:
        description: Release tag to publish to edge and stable
        required: true
        type: string

permissions:
  contents: read

jobs:
  snap-release:
    runs-on: ubuntu-latest
    env:
      SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}
      SNAPCRAFT_HAS_TTY: 'true' # Workaround for https://github.com/snapcore/snapcraft/issues/4439
      RELEASE_TAG: ${{ github.event.release.tag_name || inputs.tag_name }}

    if: ${{ github.event_name == 'workflow_dispatch' || !github.event.release.prerelease }}

    steps:
      - name: Download release snap
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          mkdir -p .tmp/snap-release
          gh release download "$RELEASE_TAG" \
            --repo "$GITHUB_REPOSITORY" \
            --pattern 'superProductivity-*.snap' \
            --dir .tmp/snap-release

          SNAP_COUNT=$(find .tmp/snap-release -maxdepth 1 -type f -name '*.snap' | wc -l | tr -d '[:space:]')
          if [ "$SNAP_COUNT" != "1" ]; then
            echo "Expected exactly one snap asset for $RELEASE_TAG, found $SNAP_COUNT"
            find .tmp/snap-release -maxdepth 1 -type f -print
            exit 1
          fi

          mv .tmp/snap-release/*.snap .tmp/snap-release/sp.snap

      # Run snapcraft from Canonical's official container so we don't depend on
      # `sudo snap install snapcraft --classic` working on the runner. The
      # container wrapper already runs `snapcraft "$@"`, so the docker CMD must
      # not include a leading `snapcraft` word.
      - name: Upload release snap to edge and stable
        uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3
        with:
          max_attempts: 5
          timeout_minutes: 15
          retry_wait_seconds: 60
          command: |
            docker run --rm \
              -v "$PWD/.tmp/snap-release:/project" \
              -e SNAPCRAFT_STORE_CREDENTIALS \
              -e SNAPCRAFT_HAS_TTY=true \
              ghcr.io/canonical/snapcraft:8_core24 \
              upload sp.snap --release edge,stable

#      - uses: actions/setup-node@v3
#        with:
#          node-version: 20
#      - name: Check out Git repository
#        uses: actions/checkout@v4
#        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: Get npm cache directory
#        id: npm-cache-dir
#        run: |
#          echo "::set-output name=dir::$(npm config get cache)"
#      - uses: actions/cache@v4
#        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 }}-node-${{ hashFiles('**/package-lock.json') }}
#          restore-keys: |
#            ${{ runner.os }}-node-
#
#      - name: Install npm Packages
#        #  if: steps.npm-cache.outputs.cache-hit != 'true'
#        run: npm i
#
#      - name: Lint
#        run: npm run lint
#
#      - name: Test Unit
#        run: npm run test
#
#      - name: Test E2E
#        run: npm run e2e
#
#      - name: Build Frontend & Electron
#        run: npm run build
#
#      - name: Install Snapcraft
#        uses: samuelmeuli/action-snapcraft@v3
#        env:
#          SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}
#
#      - name: Build/Release Electron app
#        uses: johannesjo/action-electron-builder@v1
#        with:
#          build_script_name: empty
#          github_token: ${{ secrets.github_token }}
#          release: ${{ startsWith(github.ref, 'refs/tags/v') }}
#        env:
#          SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}
#
#      - run: snapcraft upload .tmp/app-builds/superProductivity*.snap --release stable
#        env:
#          SNAPCRAFT_STORE_CREDENTIALS: ${{ secrets.SNAPCRAFT_STORE_CREDENTIALS }}
