name: Release CLI to NPM

on:
  push:
    tags: [yaak-cli-*]
  workflow_dispatch:
    inputs:
      version:
        description: CLI version to publish (for example 0.4.0 or v0.4.0)
        required: true
        type: string

permissions:
  contents: read

jobs:
  prepare-vendored-assets:
    name: Prepare vendored plugin assets
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: lts/*

      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable

      - name: Install dependencies
        run: npm ci

      - name: Build plugin assets
        env:
          SKIP_WASM_BUILD: "1"
        run: |
          npm run build
          npm run vendor:vendor-plugins

      - name: Upload vendored assets
        uses: actions/upload-artifact@v4
        with:
          name: vendored-assets
          path: |
            crates-tauri/yaak-app-client/vendored/plugin-runtime/index.cjs
            crates-tauri/yaak-app-client/vendored/plugins
          if-no-files-found: error

  build-binaries:
    name: Build ${{ matrix.pkg }}
    needs: prepare-vendored-assets
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - pkg: cli-darwin-arm64
            runner: macos-latest
            target: aarch64-apple-darwin
            binary: yaak
          - pkg: cli-darwin-x64
            runner: macos-latest
            target: x86_64-apple-darwin
            binary: yaak
          - pkg: cli-linux-arm64
            runner: ubuntu-22.04-arm
            target: aarch64-unknown-linux-gnu
            binary: yaak
          - pkg: cli-linux-x64
            runner: ubuntu-22.04
            target: x86_64-unknown-linux-gnu
            binary: yaak
          - pkg: cli-win32-arm64
            runner: windows-latest
            target: aarch64-pc-windows-msvc
            binary: yaak.exe
          - pkg: cli-win32-x64
            runner: windows-latest
            target: x86_64-pc-windows-msvc
            binary: yaak.exe

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Restore Rust cache
        uses: Swatinem/rust-cache@v2
        with:
          shared-key: release-cli-npm
          cache-on-failure: true

      - name: Install Linux build dependencies
        if: startsWith(matrix.runner, 'ubuntu')
        run: |
          sudo apt-get update
          sudo apt-get install -y pkg-config libdbus-1-dev

      - name: Download vendored assets
        uses: actions/download-artifact@v4
        with:
          name: vendored-assets
          path: crates-tauri/yaak-app-client/vendored

      - name: Set CLI build version
        shell: bash
        env:
          WORKFLOW_VERSION: ${{ inputs.version }}
        run: |
          set -euo pipefail
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            VERSION="$WORKFLOW_VERSION"
          else
            VERSION="${GITHUB_REF_NAME#yaak-cli-}"
          fi
          VERSION="${VERSION#v}"
          echo "Building yaak version: $VERSION"
          echo "YAAK_CLI_VERSION=$VERSION" >> "$GITHUB_ENV"

      - name: Build yaak
        run: cargo build --locked --release -p yaak-cli --bin yaak --target ${{ matrix.target }}

      - name: Stage binary artifact
        shell: bash
        run: |
          set -euo pipefail
          mkdir -p "npm/dist/${{ matrix.pkg }}"
          cp "target/${{ matrix.target }}/release/${{ matrix.binary }}" "npm/dist/${{ matrix.pkg }}/${{ matrix.binary }}"

      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.pkg }}
          path: npm/dist/${{ matrix.pkg }}/${{ matrix.binary }}
          if-no-files-found: error

  publish-npm:
    name: Publish @yaakapp/cli packages
    needs: build-binaries
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: lts/*
          registry-url: https://registry.npmjs.org

      - name: Download binary artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: cli-*
          path: npm/dist
          merge-multiple: false

      - name: Prepare npm packages
        shell: bash
        env:
          WORKFLOW_VERSION: ${{ inputs.version }}
        run: |
          set -euo pipefail
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            VERSION="$WORKFLOW_VERSION"
          else
            VERSION="${GITHUB_REF_NAME#yaak-cli-}"
          fi
          VERSION="${VERSION#v}"
          if [[ "$VERSION" == *-* ]]; then
            PRERELEASE="${VERSION#*-}"
            NPM_TAG="${PRERELEASE%%.*}"
          else
            NPM_TAG="latest"
          fi
          echo "Preparing CLI npm packages for version: $VERSION"
          echo "Publishing with npm dist-tag: $NPM_TAG"
          echo "NPM_TAG=$NPM_TAG" >> "$GITHUB_ENV"
          YAAK_CLI_VERSION="$VERSION" node npm/prepare-publish.js

      - name: Publish @yaakapp/cli-darwin-arm64
        run: npm publish --provenance --access public --tag "$NPM_TAG"
        working-directory: npm/cli-darwin-arm64

      - name: Publish @yaakapp/cli-darwin-x64
        run: npm publish --provenance --access public --tag "$NPM_TAG"
        working-directory: npm/cli-darwin-x64

      - name: Publish @yaakapp/cli-linux-arm64
        run: npm publish --provenance --access public --tag "$NPM_TAG"
        working-directory: npm/cli-linux-arm64

      - name: Publish @yaakapp/cli-linux-x64
        run: npm publish --provenance --access public --tag "$NPM_TAG"
        working-directory: npm/cli-linux-x64

      - name: Publish @yaakapp/cli-win32-arm64
        run: npm publish --provenance --access public --tag "$NPM_TAG"
        working-directory: npm/cli-win32-arm64

      - name: Publish @yaakapp/cli-win32-x64
        run: npm publish --provenance --access public --tag "$NPM_TAG"
        working-directory: npm/cli-win32-x64

      - name: Publish @yaakapp/cli
        run: npm publish --provenance --access public --tag "$NPM_TAG"
        working-directory: npm/cli
