name: Dev Release

on:
  workflow_dispatch:

permissions:
  id-token: write
  contents: write

env:
  NODE_VERSION: 20

jobs:
  prepare-dev:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.versions.outputs.version }}
      tag: ${{ steps.versions.outputs.tag }}
      release_name: ${{ steps.versions.outputs.release_name }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: ${{ env.NODE_VERSION }}

      - name: Compute dev versions
        id: versions
        run: |
          BASE_VERSION=$(node -p "require('./package.json').version")
          DEV_VERSION="${BASE_VERSION}-dev"
          TAG="v${DEV_VERSION}"
          echo "version=$DEV_VERSION" >> "$GITHUB_OUTPUT"
          echo "tag=$TAG" >> "$GITHUB_OUTPUT"
          echo "release_name=$TAG" >> "$GITHUB_OUTPUT"

      - name: Create GitHub release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAG: ${{ steps.versions.outputs.tag }}
        run: |
          if gh release view "$TAG" >/dev/null 2>&1; then
            echo "Release $TAG already exists"
          else
            gh release create "$TAG" --title "$TAG" --generate-notes
          fi

  build-and-upload:
    needs: prepare-dev
    uses: ./.github/workflows/build-and-upload.yml
    with:
      version: ${{ needs.prepare-dev.outputs.version }}
      tag: ${{ needs.prepare-dev.outputs.tag }}
      release_name: ${{ needs.prepare-dev.outputs.release_name }}
    secrets: inherit

  publish-server:
    needs:
      - prepare-dev 
      - build-and-upload
    uses: ./.github/workflows/manual-npm-publish.yml
    with:
      version: ${{ needs.prepare-dev.outputs.version }}
      dist_tag: dev
    secrets: inherit
