name: Publish to npm

# 트리거:
# 1) 'v*' 태그 push (예: v1.0.0)
# 2) Actions 탭에서 Run workflow (dry-run 옵션)
on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      dry_run:
        description: 'Dry run (npm publish --dry-run)'
        required: false
        type: boolean
        default: false

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20.x'
          registry-url: 'https://registry.npmjs.org'

      - name: Show npm/node versions
        run: |
          node --version
          npm --version
          npm whoami || echo "(whoami failed — 이상이 아님: token 인증은 publish 시점에 적용됨)"

      - name: Install dependencies
        run: npm install --no-audit --no-fund

      - name: Sanity check (syntax)
        run: |
          for f in index.js lib/api/SmartThingsClient.js lib/api/LegacyACClient.js \
                   lib/auth/OAuthServer.js lib/accessories/LegacyAC.js \
                   lib/accessories/SmartAC.js lib/accessories/Laundry.js; do
            node --check "$f"
          done
          node -e "JSON.parse(require('fs').readFileSync('config.schema.json','utf8'))"
          echo "All syntax checks passed."

      # v2.1.3 — 감사 제안 반영: 전 스위트(11개, 505+체크)가 통과해야만 publish 진행.
      # 실타이머 스위트 포함이라 ~6분 소요. 실패 시 publish 차단.
      - name: Verify python bridge syntax
        run: python3 -m py_compile lib/local/bridge.py

      # ⚠️cryptography 가 없으면 `bridge_cert` 스위트가 **exit 0 으로 건너뛴다** — 게이트는
      #   초록인데 계약은 한 번도 안 돌았다는 뜻이다(2026-08-05 적대 리뷰). 이 스위트는
      #   "첫 설치에서 인증서 발급이 통째로 실패하던 결함"이 사용자 로그로만 드러나서 만든
      #   것이라, skip=pass 는 목적과 정면으로 어긋난다. 설치해서 반드시 돌게 한다.
      - name: Install python deps for cert contract
        run: python3 -m pip install --quiet cryptography

      - name: Run full test suites (505+ checks)
        run: npm test

      - name: Verify package version matches tag
        if: startsWith(github.ref, 'refs/tags/v')
        run: |
          PKG_VERSION=$(node -p "require('./package.json').version")
          TAG_VERSION="${GITHUB_REF_NAME#v}"
          if [ "$PKG_VERSION" != "$TAG_VERSION" ]; then
            echo "::error::package.json version ($PKG_VERSION) does not match tag ($TAG_VERSION)"
            exit 1
          fi
          echo "Version matches: $PKG_VERSION"

      - name: Publish to npm (dry-run)
        if: github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'true'
        run: npm publish --dry-run --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Publish to npm
        if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run != 'true')
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
