# Build the punktfunk Decky Loader plugin (Gaming-Mode QAM launcher) into a distribution zip
# and publish it to Gitea's GENERIC package registry, giving Decky's "install from URL" a
# stable link. On tags the zip is ALSO attached to the Gitea release.
#
#   PUT/GET  https://git.unom.io/api/packages/unom/generic/punktfunk-decky/<version>/punktfunk.zip
#
# The plugin backend is PURE PYTHON (clients/decky/main.py — no compiled binary), so we do NOT
# need the Decky CLI (which requires Docker + rust-nightly only to compile native backends).
# We build the frontend with pnpm and assemble the store-layout zip by hand:
#
#   punktfunk.zip
#     punktfunk/                  <- single top-level dir == plugin.json "name"
#       plugin.json   [required]
#       package.json  [required; CI stamps "version" — Decky reads the installed version here]
#       main.py       [required: python backend]
#       dist/index.js [required: rollup output]
#       update.json   [CI-baked {channel, manifest}: where the plugin's self-update check polls]
#       README.md     (recommended)
#       LICENSE       [required by the plugin store]
#
# SELF-UPDATE (no Decky store): alongside the zip we also publish a tiny per-channel
# `manifest.json` ({version, artifact=<immutable per-version zip URL>, sha256}). The installed
# plugin polls it (main.py check_update), and the frontend drives Decky's own install RPC to
# apply a newer build. See clients/decky/README.md "Updating".
#
# REGISTRY_TOKEN: repo Actions secret, a PAT with write:package scope (shared with deb/rpm/docker).
name: decky

on:
  push:
    branches: [main]
    tags: ['v*']
  workflow_dispatch:

env:
  REGISTRY: git.unom.io
  OWNER: unom
  PACKAGE: punktfunk-decky        # generic-registry package name
  PLUGIN: punktfunk               # plugin.json "name" == zip top-level dir

jobs:
  build-publish:
    runs-on: ubuntu-24.04
    timeout-minutes: 30
    container:
      image: node:22-bookworm     # node + corepack(pnpm); matches the @decky toolchain
    defaults:
      run:
        working-directory: clients/decky
    steps:
      - uses: actions/checkout@v4

      - name: pnpm
        run: |
          corepack enable
          # The repo's pnpm-lock.yaml + package.json devDeps target pnpm 9 (the version the
          # @decky toolchain and the local build use). Pin it so --frozen-lockfile holds.
          corepack prepare pnpm@9 --activate

      - name: Build frontend
        run: |
          pnpm install --frozen-lockfile
          pnpm run build          # rollup -> clients/decky/dist/index.js

      - name: Version + channel + stamp
        # Tag vX.Y.Z -> X.Y.Z (stable `latest/` alias + Gitea Release); main push -> 0.3.<run>
        # (`canary/` alias). Decky reads a plugin's INSTALLED version from package.json (NOT
        # plugin.json), and the plugin's own update check (clients/decky/main.py check_update)
        # compares against it — so the build version is STAMPED into package.json here (mirrored
        # into plugin.json for store parity). Canary is a PLAIN numeric semver, never a
        # `-ci<N>` prerelease: compare-versions orders prerelease identifiers lexically
        # (ci10 < ci9), which would break update detection; the run number is monotonic.
        working-directory: ${{ gitea.workspace }}
        run: |
          case "$GITHUB_REF" in
            refs/tags/v*) V="${GITHUB_REF_NAME#v}"; ALIAS=latest ;;
            *)            V="0.3.${GITHUB_RUN_NUMBER}"; ALIAS=canary ;;
          esac
          BASE="https://$REGISTRY/api/packages/$OWNER/generic/$PACKAGE"
          echo "VERSION=$V" >> "$GITHUB_ENV"
          echo "ALIAS=$ALIAS" >> "$GITHUB_ENV"
          echo "BASE=$BASE" >> "$GITHUB_ENV"
          echo "decky version $V -> alias '$ALIAS'"
          VERSION="$V" node -e 'const fs=require("fs");for(const f of ["clients/decky/package.json","clients/decky/plugin.json"]){const j=JSON.parse(fs.readFileSync(f,"utf8"));j.version=process.env.VERSION;fs.writeFileSync(f,JSON.stringify(j,null,2)+"\n");}'

      - name: Assemble store-layout zip
        working-directory: ${{ gitea.workspace }}
        run: |
          apt-get update && apt-get install -y --no-install-recommends zip >/dev/null
          STAGE="$RUNNER_TEMP/decky"
          DEST="$STAGE/$PLUGIN"
          rm -rf "$STAGE"; mkdir -p "$DEST/dist" "$DEST/bin"
          cp clients/decky/plugin.json   "$DEST/"
          cp clients/decky/package.json  "$DEST/"
          cp clients/decky/main.py       "$DEST/"
          cp clients/decky/dist/index.js "$DEST/dist/"
          cp clients/decky/README.md     "$DEST/"
          # The stream-launch wrapper (target of the Steam shortcut); keep it executable
          # (runner_info() also re-chmods at runtime in case the zip/extract drops the bit).
          cp clients/decky/bin/punktfunkrun.sh "$DEST/bin/"
          chmod 0755 "$DEST/bin/punktfunkrun.sh"
          # Store requires a LICENSE in the plugin root; the project is MIT OR Apache-2.0.
          cp LICENSE-MIT                 "$DEST/LICENSE"
          # Self-update channel pointer the backend reads (main.py check_update). It points at
          # THIS channel's manifest.json (published below); that manifest in turn points at the
          # immutable per-version zip, so its sha256 stays valid across future alias re-uploads.
          printf '{"channel":"%s","manifest":"%s/%s/manifest.json"}\n' "$ALIAS" "$BASE" "$ALIAS" > "$DEST/update.json"
          ( cd "$STAGE" && zip -r "$RUNNER_TEMP/punktfunk.zip" "$PLUGIN" )
          ls -lh "$RUNNER_TEMP/punktfunk.zip"
          unzip -l "$RUNNER_TEMP/punktfunk.zip"
          # The update manifest the plugin polls: the immutable per-version artifact + its
          # sha256 (Decky's installer verifies the download against this hash, aborting on
          # mismatch — so it MUST be the per-version URL, never the mutable alias).
          SHA=$(sha256sum "$RUNNER_TEMP/punktfunk.zip" | cut -d' ' -f1)
          printf '{"version":"%s","artifact":"%s/%s/punktfunk.zip","sha256":"%s"}\n' \
            "$VERSION" "$BASE" "$VERSION" "$SHA" > "$RUNNER_TEMP/manifest.json"
          cat "$RUNNER_TEMP/manifest.json"

      - name: Publish to the Gitea generic registry
        working-directory: ${{ gitea.workspace }}
        env:
          TOKEN: ${{ secrets.REGISTRY_TOKEN }}
        run: |
          BASE="https://$REGISTRY/api/packages/$OWNER/generic/$PACKAGE"
          # 1) Immutable, versioned URL + its update manifest (the manifest's `artifact` points
          #    here, so the published sha256 keeps matching what Decky later downloads).
          curl -fsS --user "enricobuehler:$TOKEN" --upload-file "$RUNNER_TEMP/punktfunk.zip" \
            "$BASE/$VERSION/punktfunk.zip"
          curl -fsS --user "enricobuehler:$TOKEN" --upload-file "$RUNNER_TEMP/manifest.json" \
            "$BASE/$VERSION/manifest.json"
          echo "published $BASE/$VERSION/punktfunk.zip"
          # 2) Channel alias (stable release -> latest/, canary main build -> canary/) — the
          #    zip is the "install from URL" link; manifest.json is what the installed plugin
          #    polls for updates. The generic registry rejects re-uploading an existing
          #    version/file (409), so delete the prior alias copies first (ignore 404 on run #1).
          for f in punktfunk.zip manifest.json; do
            curl -fsS -o /dev/null --user "enricobuehler:$TOKEN" -X DELETE "$BASE/$ALIAS/$f" || true
          done
          curl -fsS --user "enricobuehler:$TOKEN" --upload-file "$RUNNER_TEMP/punktfunk.zip" \
            "$BASE/$ALIAS/punktfunk.zip"
          curl -fsS --user "enricobuehler:$TOKEN" --upload-file "$RUNNER_TEMP/manifest.json" \
            "$BASE/$ALIAS/manifest.json"
          echo "install-from-URL link: $BASE/$ALIAS/punktfunk.zip"
          echo "update manifest:       $BASE/$ALIAS/manifest.json"

      - name: Attach zip to the Gitea release (stable tags only)
        if: startsWith(gitea.ref, 'refs/tags/v')
        working-directory: ${{ gitea.workspace }}
        env:
          GITEA_TOKEN: ${{ secrets.REGISTRY_TOKEN }}
        run: |
          . scripts/ci/gitea-release.sh
          RID=$(ensure_release "$GITHUB_REF_NAME" "$GITHUB_REF_NAME" auto)
          upsert_asset "$RID" "$RUNNER_TEMP/punktfunk.zip" "punktfunk-${VERSION}.zip"
