---
name: Release Draft Create

on:
  pull_request:
    types: ['closed']
    branches: [main, core/*]
    paths:
      - 'package.json'

jobs:
  build:
    runs-on: ubuntu-latest
    if: >
      github.event.pull_request.merged == true &&
      contains(github.event.pull_request.labels.*.name, 'Release')
    outputs:
      version: ${{ steps.current_version.outputs.version }}
      is_prerelease: ${{ steps.check_prerelease.outputs.is_prerelease }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v6

      - name: Install pnpm
        uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4.4.0

      - uses: actions/setup-node@v6
        with:
          node-version-file: '.nvmrc'
          cache: 'pnpm'

      - name: Get current version
        id: current_version
        run: |
          VERSION=$(node -p "require('./package.json').version")
          echo "version=$VERSION" >> $GITHUB_OUTPUT
      - name: Check if prerelease
        id: check_prerelease
        run: |
          VERSION=${{ steps.current_version.outputs.version }}
          if [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+- ]]; then
            echo "is_prerelease=true" >> $GITHUB_OUTPUT
          else
            echo "is_prerelease=false" >> $GITHUB_OUTPUT
          fi
      - name: Build project
        env:
          SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
          ALGOLIA_APP_ID: ${{ secrets.ALGOLIA_APP_ID }}
          ALGOLIA_API_KEY: ${{ secrets.ALGOLIA_API_KEY }}
          ENABLE_MINIFY: 'true'
          USE_PROD_CONFIG: 'true'
          IS_NIGHTLY: ${{  case(github.ref == 'refs/heads/main', 'true', 'false') }}
        run: |
          pnpm install --frozen-lockfile

          # Desktop-specific release artifact with desktop distribution flags.
          DISTRIBUTION=desktop pnpm build
          pnpm zipdist ./dist ./dist-desktop.zip

          # Default release artifact for core/PyPI.
          NX_SKIP_NX_CACHE=true pnpm build
          pnpm zipdist
      - name: Upload dist artifact
        uses: actions/upload-artifact@v6
        with:
          name: dist-files
          path: |
            dist/
            dist.zip
            dist-desktop.zip

  draft_release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download dist artifact
        uses: actions/download-artifact@v7
        with:
          name: dist-files
      - name: Create release
        id: create_release
        uses: softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b # v2.5.0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          files: |
            dist.zip
            dist-desktop.zip
          tag_name: v${{ needs.build.outputs.version }}
          target_commitish: ${{ github.event.pull_request.base.ref }}
          make_latest: >-
            ${{ github.event.pull_request.base.ref == 'main' &&
                needs.build.outputs.is_prerelease == 'false' }}
          draft: >-
            ${{ github.event.pull_request.base.ref != 'main' ||
                needs.build.outputs.is_prerelease == 'true' }}
          prerelease: >-
            ${{ needs.build.outputs.is_prerelease == 'true' }}
          generate_release_notes: true

  publish_types:
    needs: build
    uses: ./.github/workflows/release-npm-types.yaml
    with:
      version: ${{ needs.build.outputs.version }}
      ref: ${{ github.event.pull_request.merge_commit_sha }}
    secrets: inherit

  comment_release_summary:
    name: Comment Release Summary
    needs:
      - draft_release
      - publish_types
    if: success()
    runs-on: ubuntu-latest
    permissions:
      contents: read
      issues: write
      pull-requests: write
    steps:
      - name: Checkout merge commit
        uses: actions/checkout@v6
        with:
          ref: ${{ github.event.pull_request.merge_commit_sha }}
          fetch-depth: 2

      - name: Post release summary comment
        uses: ./.github/actions/comment-release-links
        with:
          issue-number: ${{ github.event.pull_request.number }}
          version_file: package.json
