name: Release

# Manual release: from the GitHub "Actions" tab pick "Release", click
# "Run workflow", enter the version tag, and it builds the Windows installer
# .exe, then publishes a GitHub Release with that artifact attached and
# auto-generated notes.
on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Release tag, e.g. v0.3.2.1 (the tag is created at the current commit on this branch)'
        required: true
        type: string
      prerelease:
        description: 'Mark this release as a pre-release'
        required: false
        default: false
        type: boolean

# Needed for the release job to create the tag and the GitHub Release.
permissions:
  contents: write

jobs:
  build-windows:
    name: Build Windows installer
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          cache: npm

      - name: Install dependencies
        run: npm ci

      # Stamp the requested version onto package.json so the produced artifact
      # carries it. Not committed back — it only affects this build.
      - name: Set version from input
        shell: bash
        env:
          VERSION: ${{ inputs.version }}
        run: node scripts/stamp-version.js "${VERSION#v}"

      - name: Configure Windows code signing
        shell: bash
        env:
          WIN_CSC_LINK: ${{ secrets.WIN_CSC_LINK }}
          WIN_CSC_KEY_PASSWORD: ${{ secrets.WIN_CSC_KEY_PASSWORD }}
        run: |
          if [ -n "$WIN_CSC_LINK" ] && [ -n "$WIN_CSC_KEY_PASSWORD" ]; then
            {
              printf 'WIN_CSC_LINK=%s\n' "$WIN_CSC_LINK"
              printf 'WIN_CSC_KEY_PASSWORD=%s\n' "$WIN_CSC_KEY_PASSWORD"
            } >> "$GITHUB_ENV"
          else
            echo "Windows code signing secrets not configured; building unsigned."
          fi

      - name: Package Windows installer .exe
        run: npm run package:windows

      - uses: actions/upload-artifact@v4
        with:
          name: windows-installer
          path: releases/*.exe
          if-no-files-found: error

  release:
    name: Publish GitHub Release
    needs: [build-windows]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Download build artifacts
        uses: actions/download-artifact@v4
        with:
          path: dist

      - name: List downloaded artifacts
        run: find dist -type f -printf '%s\t%p\n'

      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ github.token }}
          TAG: ${{ inputs.version }}
          PRERELEASE: ${{ inputs.prerelease }}
        run: |
          flags=()
          if [ "$PRERELEASE" = "true" ]; then
            flags+=(--prerelease)
          fi
          gh release create "$TAG" \
            dist/windows-installer/*.exe \
            --title "StepForge $TAG" \
            --target "${{ github.sha }}" \
            --generate-notes \
            "${flags[@]}"
