name: Publish to NPM

on:
  push:
    tags:
      - "v*"

permissions:
  id-token: write
  contents: read

jobs:
  publish:
    name: Build and publish package
    runs-on: ubuntu-latest
    environment:
      name: npm
      url: https://npmjs.com/package/@dexalot/dexalot-sdk

    steps:
      - name: Check out repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Set up Node.js
        uses: actions/setup-node@v6
        with:
          node-version: "22"
          registry-url: https://registry.npmjs.org

      # Node’s prebundled npm can be incomplete on runners (e.g. missing
      # promise-retry); bootstrap from the registry tarball, then upgrade.
      - name: Update npm to 11.12.1
        run: |
          curl -fsSL "https://registry.npmjs.org/npm/-/npm-11.12.1.tgz" -o /tmp/npm.tgz
          tar -xzf /tmp/npm.tgz -C /tmp
          node /tmp/package/bin/npm-cli.js install -g npm@11.12.1

      - name: Ensure workflow runs from a version tag
        env:
          GITHUB_REF_TYPE: ${{ github.ref_type }}
          GITHUB_REF_NAME: ${{ github.ref_name }}
        run: |
          if [ "$GITHUB_REF_TYPE" != "tag" ]; then
            echo "Refusing to publish: workflow must run from a git tag, got $GITHUB_REF_TYPE:$GITHUB_REF_NAME"
            exit 1
          fi

      - name: Ensure tag commit is on main
        run: |
          git fetch origin main --depth=1
          if ! git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then
            echo "Refusing to publish: tagged commit $GITHUB_SHA is not reachable from origin/main"
            exit 1
          fi

      - name: Validate tag matches package version
        env:
          GITHUB_REF_NAME: ${{ github.ref_name }}
        run: |
          node - <<'JS'
          const fs = require("node:fs");

          const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));
          const version = pkg.version;
          const tag = process.env.GITHUB_REF_NAME;
          const expectedTag = `v${version}`;

          if (tag !== expectedTag) {
            throw new Error(
              `Refusing to publish: git tag ${JSON.stringify(tag)} does not match project version ${JSON.stringify(expectedTag)}`
            );
          }

          console.log(`Publishing @dexalot/dexalot-sdk version ${version} from tag ${tag}`);
          JS

      - name: Install dependencies
        run: npm ci

      # Pre-publish CVE gates: refuse to ship a tag that introduces
      # known high-severity advisories. Mirrors the Python SDK's
      # `pip-audit --strict` in `pypi.yml`.
      - name: npm audit (high+ severity gate)
        run: npm audit --audit-level=high

      - name: OSV scanner
        uses: google/osv-scanner-action/osv-scanner-action@v2.1.0
        with:
          scan-args: |
            --lockfile=package-lock.json

      - name: Type check
        run: npx tsc --noEmit -p tsconfig.build.json

      - name: Unit tests with 100% coverage gate
        run: npx jest --ci tests/unit

      - name: Build distributions
        run: npm run build

      - name: Validate package contents
        run: npm pack --dry-run

      - name: Publish to NPM
        run: npm publish --provenance
