name: Publish

on:
  push:
    tags: ["v*"]

jobs:
  publish:
    runs-on: ubuntu-latest

    permissions:
      contents: write
      id-token: write

    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Install SQLite
        run: sudo apt-get update && sudo apt-get install -y libsqlite3-dev

      - run: bun install
      - run: bun test --timeout 30000 --preload ./src/test-preload.ts test/
        env:
          CI: true
          LD_LIBRARY_PATH: /usr/lib/x86_64-linux-gnu

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          registry-url: https://registry.npmjs.org

      - run: npm run build
      - run: npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Extract release notes
        id: notes
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          NOTES=$(./scripts/extract-changelog.sh "$VERSION")
          # Write to file for gh release (avoids quoting issues)
          echo "$NOTES" > /tmp/release-notes.md

      - name: Create GitHub release
        run: |
          gh release create "$GITHUB_REF_NAME" \
            --title "$GITHUB_REF_NAME" \
            --notes-file /tmp/release-notes.md
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
