name: Publish to NPM

on:
  push:
    tags:
      - "v*.*.*"

permissions:
  id-token: write
  contents: write

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    steps:
      - name: Check out Git repository
        uses: actions/checkout@v6

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: "24"
          registry-url: "https://registry.npmjs.org/"

      - uses: actions/cache@v5
        name: Setup npm cache
        with:
          path: ~/.npm
          key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Build
        run: npm run build

      - name: Publish to NPM
        run: npm publish

  release:
    needs: publish-npm
    runs-on: ubuntu-latest
    steps:
      - name: Check out Git repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Generate release notes
        uses: orhun/git-cliff-action@v4
        with:
          args: --latest --strip header
        env:
          OUTPUT: CHANGES.md
          GITHUB_REPO: ${{ github.repository }}

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          body_path: CHANGES.md

      - name: Generate full changelog
        uses: orhun/git-cliff-action@v4
        with:
          args: --output CHANGELOG.md
        env:
          GITHUB_REPO: ${{ github.repository }}

      - name: Commit changelog to main
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git checkout main
          git add CHANGELOG.md
          git diff --cached --quiet || git commit -m "chore: update changelog for ${{ github.ref_name }} [skip ci]"
          git push origin main
