name: Release

on:
  workflow_dispatch:
    inputs:
      dry_run:
        description: 'Dry run (仅预览，不实际发布)'
        type: boolean
        default: false
        required: false

permissions:
  contents: write
  issues: write
  pull-requests: write
  id-token: write

jobs:
  release-core:
    name: Release Core Package
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false
          token: ${{ secrets.GITHUB_TOKEN }}

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

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: |
          cd packages/core
          npm run test:ci

      - name: Build package
        run: |
          cd packages/core
          npm run build:npm

      - name: Release (Dry Run)
        if: ${{ github.event.inputs.dry_run == 'true' }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          cd packages/core
          npx semantic-release --dry-run

      - name: Release
        if: ${{ github.event.inputs.dry_run != 'true' }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          cd packages/core
          npx semantic-release
