name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write
  issues: write
  pull-requests: read

jobs:
  release:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Full history for changelog generation

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Lint runtime console usage
        run: npm run lint:console

      - name: Build
        run: npm run build

      - name: Run tests
        run: npm run test:run

      - name: Generate release notes
        id: notes
        run: |
          # Get the previous tag (if any)
          PREV_TAG=$(git tag --sort=-creatordate | sed -n '2p')
          CURRENT_TAG=${GITHUB_REF#refs/tags/}

          if [ -z "$PREV_TAG" ]; then
            echo "First release - including all commits"
            RANGE="HEAD"
          else
            RANGE="${PREV_TAG}..${CURRENT_TAG}"
          fi

          # Collect merged PRs from commit messages
          echo "## What's Changed" > notes.md
          echo "" >> notes.md

          # Extract PR numbers and titles from merge commits
          git log $RANGE --oneline --grep="(#" | while read -r line; do
            # Extract PR number
            PR_NUM=$(echo "$line" | grep -oP '\(#\K[0-9]+' | head -1)
            # Clean up the message (remove hash prefix)
            MSG=$(echo "$line" | sed 's/^[a-f0-9]* //')
            if [ -n "$PR_NUM" ]; then
              echo "- ${MSG} by @$(gh pr view $PR_NUM --json author --jq '.author.login' 2>/dev/null || echo 'contributor')" >> notes.md
            else
              echo "- ${MSG}" >> notes.md
            fi
          done

          # Add install instructions
          echo "" >> notes.md
          echo "## Install" >> notes.md
          echo "" >> notes.md
          echo '```bash' >> notes.md
          echo "npx lettabot onboard" >> notes.md
          echo "# or" >> notes.md
          echo "npm install -g lettabot" >> notes.md
          echo '```' >> notes.md

          # Add full changelog link
          if [ -n "$PREV_TAG" ]; then
            echo "" >> notes.md
            echo "**Full Changelog**: https://github.com/letta-ai/lettabot/compare/${PREV_TAG}...${CURRENT_TAG}" >> notes.md
          fi

          cat notes.md
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Create GitHub Release
        run: |
          CURRENT_TAG=${GITHUB_REF#refs/tags/}

          # Determine if pre-release
          if echo "$CURRENT_TAG" | grep -qE '(alpha|beta|rc)'; then
            PRERELEASE="--prerelease"
          else
            PRERELEASE=""
          fi

          gh release create "$CURRENT_TAG" \
            --title "$CURRENT_TAG" \
            --notes-file notes.md \
            $PRERELEASE
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish to npm
        if: env.NPM_TOKEN != ''
        run: |
          CURRENT_TAG=${GITHUB_REF#refs/tags/}
          echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc

          # Set version from git tag (strip 'v' prefix)
          VERSION=${CURRENT_TAG#v}
          npm version "$VERSION" --no-git-tag-version --allow-same-version

          # Determine npm tag (pre-releases get 'next', stable gets 'latest')
          if echo "$CURRENT_TAG" | grep -qE '(alpha|beta|rc)'; then
            npm publish --tag next --access public
          else
            npm publish --access public
          fi
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
