// Add this webhook in Gitea:
// http://192.168.1.37:8080/gitea-webhook/post

pipeline {
    agent { label 'rust_no_docker' }

    environment {
        CARGO_TERM_COLOR = 'always'
        RUST_BACKTRACE = '1'
    }

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

                sh '''
                    set -eu

                    git submodule sync --recursive
                    git submodule update --init --recursive
                '''
            }
        }

        stage('Setup Rust') {
            steps {
                sh '''
                    set -eu

                    if ! command -v rustup >/dev/null 2>&1; then
                        echo "ERROR: rustup is not installed on this worker." >&2
                        exit 1
                    fi

                    echo "Installing/updating repository toolchain..."
                    rustup show

                    # The repository's .rust-toolchain.toml selects the
                    # appropriate Rust version automatically.
                    rustup toolchain install 1.92.0 \
                        --profile minimal \
                        --component rustfmt \
                        --component clippy

                    echo
                    echo "Using:"
                    rustc --version
                    cargo --version
                    rustfmt --version
                    cargo clippy --version
                '''
            }
        }

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

                    echo "Checking Rust formatting..."
                    cargo fmt --all -- --check
                '''
            }
        }

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

                    echo "Checking all workspace targets..."
                    cargo check \
                        --workspace \
                        --all-targets \
                        --all-features \
                        --locked
                '''
            }
        }

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

                    echo "Running workspace tests..."
                    cargo test \
                        --workspace \
                        --all-targets \
                        --all-features \
                        --locked
                '''
            }
        }

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

                    echo "Running Clippy..."
                    cargo clippy \
                        --workspace \
                        --all-targets \
                        --all-features \
                        --locked
                '''
            }
        }

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

                    echo "Building release binaries..."
                    cargo build \
                        --workspace \
                        --release \
                        --all-features \
                        --locked

                    echo
                    echo "Release build completed."
                '''
            }
        }
    }

    post {
        success {
            publishChecks(
                conclusion: 'SUCCESS',
                name: 'Rust CI',
                summary: 'Epoch Forge passed all CI checks.',
                text: 'Formatting, workspace checks, tests, Clippy, and the release build completed successfully.',
                title: 'Build Successful'
            )

            echo '✅ Epoch Forge passed all CI checks.'
        }

        failure {
            publishChecks(
                conclusion: 'FAILURE',
                name: 'Rust CI',
                summary: 'Epoch Forge failed CI.',
                text: 'Formatting, compilation, tests, Clippy, or the release build failed. Check the Jenkins log.',
                title: 'Build Failed'
            )

            echo '❌ Epoch Forge CI failed.'
        }

        unstable {
            publishChecks(
                conclusion: 'ACTION_REQUIRED',
                name: 'Rust CI',
                summary: 'Epoch Forge CI is unstable.',
                text: 'The Jenkins build completed with an unstable status.',
                title: 'Build Unstable'
            )

            echo '⚠️ Epoch Forge CI is unstable.'
        }
    }
}