def deploy() {
    node('ubuntu') {
        try {
            stage('Deploy (Ubuntu)') {
                checkout scm
                nodejs(nodeJSInstallationName: 'Node23') {
                    sh 'yarn install --frozen-lockfile'
                    sh 'yarn build'
                    // Deploy to Cloudflare Pages using wrangler
                    // CLOUDFLARE_API_TOKEN should be set as a Jenkins credential/env variable
                    sh 'npx wrangler pages deploy build'
                }
            }
        } finally {
            cleanWs()
        }
    }
}

def buildOnLabel(label, buildCommand) {
    return {
        node(label) {
            env.NODE_ENV = 'production'
            try {
                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'
                        } else {
                            bat 'yarn install --frozen-lockfile'
                        }
                    }
                }

                stage("Build (${label})") {
                    nodejs(nodeJSInstallationName: 'Node23') {
                        if (isUnix()) {
                            sh buildCommand
                            sh 'ls -la build || echo "Build directory not found"'
                        } else {
                            bat buildCommand
                            bat 'if not exist build echo "Build directory not found"'
                        }
                    }
                }

                stage("Verify Build (${label})") {
                    if (isUnix()) {
                        sh 'test -d build || (echo "Build directory does not exist" && exit 1)'
                    } else {
                        bat 'if not exist build (echo "Build directory does not exist" && exit 1)'
                    }
                }
            } finally {
                cleanWs()
            }
        }
    }
}

try {
    parallel(
        'Windows Build': buildOnLabel('windows', 'yarn build:electron'),
        'MacOS Build': buildOnLabel('macos', 'yarn build:electron'),
        'Ubuntu Deploy': { deploy() }
    )

    echo 'All parallel stages completed successfully!'

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