name: NPM Publish
on:
  pull_request:
    types: [closed]
    branches:
      - master
      - next
      - next-ng
      - v7
  workflow_dispatch: # Allow manual trigger from any branch

permissions:
  contents: read
  id-token: write # Required for npm Trusted Publishing (OIDC)

jobs:
  publish:
    if: >-
      github.event_name == 'workflow_dispatch' ||
      (github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'release-please'))
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 'lts/krypton' # Node 24 LTS
      - name: Install dependencies
        run: npm install
      - name: Build
        run: npm run build
      - name: Publish to npm
        run: npm publish
        # No NODE_AUTH_TOKEN needed — uses OIDC Trusted Publishing
      - name: Read package.json version
        id: package-version
        run: echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
      - name: Push Notification to Google Workspace
        run: |
          curl -X POST "https://chat.googleapis.com/v1/spaces/AAAA4hvxkZc/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=_DSEU_3n-CC5ZDK2nKNfabm2X_M_IcIAqQxenkYgDZk" \
          -H 'Content-Type: application/json' \
          -d '{
            "text": "@dexteel/mesf-core has been deployed :\n\nREPO: *${{ github.repository }}*\nCOMMIT: ${{ github.sha }}\nVERSION: ${{ steps.package-version.outputs.version }}\nJOB: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
          }'
