name: Create Protobuf Release for JSR

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version to publish (e.g. v1.2.3). Used when manually dispatching."
        required: true
        type: string
      dry_run:
        description: "Don't actually publish to JSR (passes --dry-run)."
        required: false
        type: boolean
        default: false

permissions: write-all

env:
  PROTOBUF_DIR: ./packages/protobufs 

jobs:
  codegen:
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Show files exist
        run: |
          set -euxo pipefail
          ls -la $PROTOBUF_DIR/packages/ts || true
          cat $PROTOBUF_DIR/packages/ts/deno.json

      - name: Determine VERSION
        run: |
          set -euo pipefail
          if [ -n "${{ inputs.version }}" ]; then
            VERSION="${{ inputs.version }}"
          else
            echo "No 'version' input. Provide inputs.version." >&2
            exit 1
          fi
          STRIPPED="${VERSION#v}"
          echo "VERSION=$STRIPPED" >> "$GITHUB_ENV"
          echo "Resolved VERSION=$STRIPPED"

      - name: Set Package Versions to current tag
        working-directory: ${{ env.PROTOBUF_DIR }}/packages/ts
        run: |
          set -euxo pipefail
          for f in deno.json; do
            test -f "$f"
            jq --arg version "${VERSION//\//-}" '
              walk(
                if type == "string" and test("__PACKAGE_VERSION__")
                then sub("__PACKAGE_VERSION__"; $version)
                else .
                end
              )
            ' "$f" > "$f.tmp"
            mv "$f.tmp" "$f"
          done

      - name: Setup Buf
        uses: bufbuild/buf-setup-action@main
        with:
          github_token: ${{ github.token }}

      - name: Setup pnpm
        uses: pnpm/action-setup@v6
      
      - name: Install workspace dependencies
        run: pnpm install --frozen-lockfile

      - name: Generate code
        run: pnpm --filter @meshtastic/protobufs-ws build

      - name: Move generated .ts files and clean up
        run: |
          set -euxo pipefail
          SRC_DIR="$PROTOBUF_DIR/packages/ts/dist/meshtastic"
          DEST_DIR="$PROTOBUF_DIR/packages/ts/dist"

          if [ -d "$SRC_DIR" ]; then
            shopt -s nullglob
            ts_files=("$SRC_DIR"/*.ts)
            if [ ${#ts_files[@]} -gt 0 ]; then
              mv "$SRC_DIR"/*.ts "$DEST_DIR"/
            fi
            rm -rf "$SRC_DIR"
          else
            echo "Source directory not found: $SRC_DIR" >&2
            exit 1
          fi

          # Remove nanopb_pb.ts if present
          if [ -f "$DEST_DIR/nanopb_pb.ts" ]; then
            rm "$DEST_DIR/nanopb_pb.ts"
          fi

      - name: Copy license & README
        run: |
          cp LICENSE $PROTOBUF_DIR/packages/ts
          cp README.md $PROTOBUF_DIR/packages/ts

      - name: Upload TypeScript code
        uses: actions/upload-artifact@v7
        with:
          name: ts_code
          path: ${{ env.PROTOBUF_DIR }}/packages/ts

  publish-jsr:
    runs-on: ubuntu-24.04
    needs: codegen
    permissions:
      contents: read
      id-token: write
    env:
      DRY_RUN: ${{ inputs.dry_run }}
    steps:
      - name: Download TypeScript code
        uses: actions/download-artifact@v8
        with:
          name: ts_code

      - name: Set up Deno
        uses: denoland/setup-deno@main
        with:
          deno-version: rc

      - name: Publish to JSR
        run: |
          set -euxo pipefail
          FLAGS="--unstable-sloppy-imports"
          if [ "${DRY_RUN}" = "true" ]; then
            FLAGS="$FLAGS --dry-run"
            echo "Running publish in dry-run mode..."
          fi
          deno publish $FLAGS
