def buildOnLabel(label, buildCommand) {
    return {
        node(label) {
            stage("Checkout (${label})") {
                checkout scm
            }

            stage("Setup Node.js (${label})") {
                nodejs(nodeJSInstallationName: 'Node23') {
                    if (isUnix()) {
                        sh 'node -v'
                        sh 'yarn -v'
                    } else {
                        bat 'node -v'
                        bat 'yarn -v'
                    }
                }
            }

            stage("Install Dependencies (${label})") {
                nodejs(nodeJSInstallationName: 'Node23') {
                    if (isUnix()) {
                        sh 'yarn install --frozen-lockfile --production=false'
                    } else {
                        bat 'yarn install --frozen-lockfile --production=false'
                    }
                }
            }

            stage("Build (${label})") {
                nodejs(nodeJSInstallationName: 'Node23') {
                    if (isUnix()) {
                        sh "NODE_ENV=production ${buildCommand}"
                    } else {
                        bat "set NODE_ENV=production && ${buildCommand}"
                    }
                }
            }

            stage("Archive Artifacts (${label})") {
                archiveArtifacts artifacts: 'app_dist/**/*.dmg, app_dist/**/*.exe, app_dist/**/*.AppImage, app_dist/**/*.deb, app_dist/**/*.rpm', fingerprint: true
            }
        }
    }
}

try {
    parallel(
        'Windows Build': buildOnLabel('windows', 'yarn build:electron'),
        'MacOS Build': buildOnLabel('macos', 'yarn build:electron'),
        'Linux Build': buildOnLabel('ubuntu', 'yarn build:electron')
    )

    echo 'All parallel stages completed successfully!'

} catch (Exception e) {
    echo "Pipeline failed: ${e.message}"
    throw e
}
