name: NPM Publish

on:
  push:
    branches:
      - dev
    paths:
      - "sdk/core/package.json"
      - "sdk/qrcode/package.json"
      - "common/package.json"
      - "packages/mobile-sdk-alpha/package.json"
      - "sdk/qrcode-angular/package.json"
      - "contracts/package.json"
  workflow_dispatch:

permissions:
  id-token: write # Required for OIDC
  contents: read

jobs:
  detect-changes:
    runs-on: ubuntu-latest
    outputs:
      core_changed: ${{ steps.check-version.outputs.core_changed }}
      qrcode_changed: ${{ steps.check-version.outputs.qrcode_changed }}
      common_changed: ${{ steps.check-version.outputs.common_changed }}
      contracts_changed: ${{ steps.check-version.outputs.contracts_changed }}
      qrcode_angular_changed: ${{ steps.check-version.outputs.qrcode_angular_changed }}
      msdk_changed: ${{ steps.check-version.outputs.msdk_changed }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 2

      - name: Check for version changes
        id: check-version
        run: |
          git diff HEAD^ HEAD --name-only | grep -q "sdk/core/package.json" && echo "core_changed=true" >> $GITHUB_OUTPUT || echo "core_changed=false" >> $GITHUB_OUTPUT
          git diff HEAD^ HEAD --name-only | grep -q "sdk/qrcode/package.json" && echo "qrcode_changed=true" >> $GITHUB_OUTPUT || echo "qrcode_changed=false" >> $GITHUB_OUTPUT
          git diff HEAD^ HEAD --name-only | grep -q "common/package.json" && echo "common_changed=true" >> $GITHUB_OUTPUT || echo "common_changed=false" >> $GITHUB_OUTPUT
          git diff HEAD^ HEAD --name-only | grep -q "contracts/package.json" && echo "contracts_changed=true" >> $GITHUB_OUTPUT || echo "contracts_changed=false" >> $GITHUB_OUTPUT
          git diff HEAD^ HEAD --name-only | grep -q "sdk/qrcode-angular/package.json" && echo "qrcode_angular_changed=true" >> $GITHUB_OUTPUT || echo "qrcode_angular_changed=false" >> $GITHUB_OUTPUT
          git diff HEAD^ HEAD --name-only | grep -q "packages/mobile-sdk-alpha/package.json" && echo "msdk_changed=true" >> $GITHUB_OUTPUT || echo "msdk_changed=false" >> $GITHUB_OUTPUT

          # check if it was dispatched manually as well
          if git diff HEAD^ HEAD -- sdk/core/package.json | grep -q '"version":' || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "core_changed=true" >> $GITHUB_OUTPUT
          fi

          if git diff HEAD^ HEAD -- sdk/qrcode/package.json | grep -q '"version":' || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "qrcode_changed=true" >> $GITHUB_OUTPUT
          fi

          if git diff HEAD^ HEAD -- common/package.json | grep -q '"version":' || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "common_changed=true" >> $GITHUB_OUTPUT
          fi

          if git diff HEAD^ HEAD -- contracts/package.json | grep -q '"version":' || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "contracts_changed=true" >> $GITHUB_OUTPUT
          fi

          if git diff HEAD^ HEAD -- sdk/qrcode-angular/package.json | grep -q '"version":' || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "qrcode_angular_changed=true" >> $GITHUB_OUTPUT
          fi

          if git diff HEAD^ HEAD -- packages/mobile-sdk-alpha/package.json | grep -q '"version":' || [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "msdk_changed=true" >> $GITHUB_OUTPUT
          fi

  publish-core:
    needs: detect-changes
    if: needs.detect-changes.outputs.core_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          registry-url: "https://registry.npmjs.org"

      - name: Install Dependencies
        uses: ./.github/actions/yarn-install

      - name: Build package
        run: |
          yarn workspace @selfxyz/core build:deps

      - name: Publish to npm
        working-directory: sdk/core
        run: |
          yarn config set npmPublishAccess public
          yarn npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  publish-qrcode:
    needs: detect-changes
    if: needs.detect-changes.outputs.qrcode_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          registry-url: "https://registry.npmjs.org"

      - name: Install Dependencies
        uses: ./.github/actions/yarn-install

      - name: Build package
        run: |
          yarn workspace @selfxyz/qrcode build:deps

      - name: Publish to npm
        working-directory: sdk/qrcode
        run: |
          yarn config set npmPublishAccess public
          yarn npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  publish-common:
    needs: detect-changes
    if: needs.detect-changes.outputs.common_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          registry-url: "https://registry.npmjs.org"
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install

      - name: Build package
        run: |
          yarn workspace @selfxyz/common build

      - name: Publish to npm
        working-directory: common
        run: |
          yarn config set npmPublishAccess public
          yarn npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  publish-contracts:
    needs: detect-changes
    if: needs.detect-changes.outputs.contracts_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          registry-url: "https://registry.npmjs.org"
      - name: Install Dependencies
        uses: ./.github/actions/yarn-install
      - name: Build package
        run: |
          yarn workspace @selfxyz/contracts build
      - name: Publish to npm
        working-directory: contracts
        run: |
          yarn config set npmPublishAccess public
          yarn npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  publish-qrcode-angular:
    needs: detect-changes
    if: needs.detect-changes.outputs.qrcode_angular_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          registry-url: "https://registry.npmjs.org"

      - name: Install Dependencies
        uses: ./.github/actions/yarn-install

      - name: Build package
        run: |
          yarn workspace @selfxyz/qrcode-angular build:deps

      - name: Publish to npm
        working-directory: sdk/qrcode-angular
        run: |
          yarn config set npmPublishAccess public
          yarn npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  publish-msdk:
    needs: detect-changes
    if: needs.detect-changes.outputs.msdk_changed == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          registry-url: "https://registry.npmjs.org"

      - name: Install Dependencies
        uses: ./.github/actions/yarn-install

      - name: Build package dependencies
        run: |
          yarn workspace @selfxyz/common build
          yarn workspace @selfxyz/mobile-sdk-alpha build

      - name: Publish to npm
        working-directory: packages/mobile-sdk-alpha
        run: |
          yarn config set npmPublishAccess restricted
          yarn npm publish --access restricted --tag alpha
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
