name: Prerelease

on:
    workflow_dispatch:
        inputs:
            channel:
                description: Prerelease channel
                required: true
                default: beta
                type: choice
                options:
                    - beta
                    - nightly
            version:
                description: Exact semantic prerelease version. Required for beta; optional for nightly.
                required: false
                type: string
    schedule:
        - cron: '0 18 * * *'

permissions:
    contents: read

concurrency:
    group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
    cancel-in-progress: true

jobs:
    resolve:
        name: Resolve prerelease metadata
        runs-on: ubuntu-latest
        outputs:
            channel: ${{ steps.release.outputs.channel }}
            version: ${{ steps.release.outputs.version }}
            tag: ${{ steps.release.outputs.tag }}
            prerelease: ${{ steps.release.outputs.prerelease }}
            release_name: ${{ steps.release.outputs.release_name }}
        steps:
            - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
              with:
                  fetch-depth: 0

            - uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6
              with:
                  node-version: 22

            - name: Resolve prerelease metadata
              id: release
              working-directory: apps/desktop
              env:
                  RELEASE_CHANNEL: ${{ github.event.inputs.channel }}
                  RELEASE_VERSION: ${{ github.event.inputs.version }}
              run: node scripts/ci/resolve-release-metadata.mjs

    publish-release:
        name: Publish prerelease release
        needs: resolve
        permissions:
            contents: write
            deployments: write
        uses: ./.github/workflows/velopack-build.yml
        with:
            channel: ${{ needs.resolve.outputs.channel }}
            version: ${{ needs.resolve.outputs.version }}
            tag: ${{ needs.resolve.outputs.tag }}
            prerelease: ${{ needs.resolve.outputs.prerelease }}
            target-commitish: ${{ github.sha }}
            deploy-production: ${{ github.ref_name == 'main' }}
            deployment-environment: prerelease
        secrets: inherit
