pipeline {
    agent any

    environment {
        CARGO_TERM_COLOR = 'never'
        RUST_BACKTRACE = '1'
        RUSTUP_TOOLCHAIN = 'stable'
    }

    options {
        skipDefaultCheckout(true)
        timestamps()
        timeout(time: 30, unit: 'MINUTES')
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Toolchain') {
            steps {
                sh '''
                    set -eu

                    for tool in git cargo rustc; do
                        if ! command -v "${tool}" >/dev/null 2>&1; then
                            echo "ERROR: ${tool} is required on the Jenkins worker." >&2
                            exit 1
                        fi
                    done

                    git --version
                    rustc --version
                    cargo --version
                    cargo clippy --version
                    cargo metadata --locked --no-deps --format-version 1 >/dev/null
                '''
            }
        }

        stage('Tests') {
            steps {
                sh '''
                    set -eu

                    cargo test --release --all-targets --locked
                '''
            }
        }

        stage('Clippy') {
            steps {
                sh '''
                    set -eu

                    cargo clippy --all-targets --locked -- -D warnings
                '''
            }
        }

        stage('Engine verifier') {
            steps {
                sh '''
                    set -eu

                    cargo run --release --locked --example verify -- 100000
                '''
            }
        }

        stage('Build') {
            steps {
                sh '''
                    set -eu

                    rm -f target/release/balatro-seed-search
                    cargo build --release --locked --bin balatro-seed-search
                    test -f target/release/balatro-seed-search
                    test -s target/release/balatro-seed-search
                '''
            }
        }

        stage('Archive') {
            steps {
                archiveArtifacts(
                    artifacts: 'target/release/balatro-seed-search',
                    fingerprint: true
                )
            }
        }
    }

    post {
        success {
            script {
                try {
                    publishChecks(
                        conclusion: 'SUCCESS',
                        name: 'Rust CI',
                        summary: 'balatro-seed-search passed CI.',
                        text: 'Release tests, Clippy, the engine verifier, and the release build passed; the native binary was archived.',
                        title: 'Build Successful'
                    )
                } catch (err) {
                    echo "Could not publish the successful Rust CI check: ${err}"
                }
            }
            echo 'balatro-seed-search passed CI.'
        }

        failure {
            script {
                try {
                    publishChecks(
                        conclusion: 'FAILURE',
                        name: 'Rust CI',
                        summary: 'balatro-seed-search failed CI.',
                        text: 'A toolchain, test, Clippy, verifier, build, or archive stage failed. Check the Jenkins log.',
                        title: 'Build Failed'
                    )
                } catch (err) {
                    echo "Could not publish the failed Rust CI check: ${err}"
                }
            }
            echo 'balatro-seed-search CI failed.'
        }

        unstable {
            script {
                try {
                    publishChecks(
                        conclusion: 'ACTION_REQUIRED',
                        name: 'Rust CI',
                        summary: 'balatro-seed-search CI is unstable.',
                        text: 'The Jenkins build completed with an unstable status. Check the Jenkins log.',
                        title: 'Build Unstable'
                    )
                } catch (err) {
                    echo "Could not publish the unstable Rust CI check: ${err}"
                }
            }
            echo 'balatro-seed-search CI is unstable.'
        }
    }
}
