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

pipeline {
    agent { label 'rust_no_docker' }

    environment {
        VENV = '.venv'
    }

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

                sh '''
                    set -eu

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

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

                    # ---------------------------------------------------------
                    # uv
                    # ---------------------------------------------------------

                    if ! command -v uv >/dev/null 2>&1; then
                        echo "uv not found — installing it..."

                        if ! command -v cargo-binstall >/dev/null 2>&1; then
                            echo "cargo-binstall not found — installing it..."
                            cargo install cargo-binstall
                        fi

                        cargo binstall --no-confirm uv
                    else
                        echo "uv already installed."
                    fi

                    echo "Using:"
                    uv --version

                    # ---------------------------------------------------------
                    # SQLite
                    # ---------------------------------------------------------

                    if ! command -v sqlite3 >/dev/null 2>&1; then
                        echo "sqlite3 not found — installing it..."
                        pkg install -y sqlite3
                    else
                        echo "sqlite3 already installed."
                    fi

                    echo "SQLite:"
                    sqlite3 --version

                    # ---------------------------------------------------------
                    # Python environment
                    # ---------------------------------------------------------

                    rm -rf "${VENV}"

                    echo "Creating virtual environment..."
                    uv venv "${VENV}"

                    echo "Installing CI Python dependencies..."
                    uv pip install \
                        --python "${VENV}/bin/python" \
                        -r tools/requirements-ci.txt

                    echo
                    echo "Python:"
                    "${VENV}/bin/python" --version

                    echo
                    echo "Environment ready."
                '''
            }
        }

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

                    echo "Running Ravenous database integrity checks..."
                    echo

                    "${VENV}/bin/python" \
                        tools/check_databases.py
                '''
            }
        }
    }

    post {
        success {
            publishChecks(
                conclusion: 'SUCCESS',
                name: 'Database Integrity',
                summary: 'All Ravenous databases passed integrity checks.',
                text: 'SQLite structural integrity and foreign-key validation completed successfully.',
                title: 'Database integrity successful'
            )

            echo '✅ All Ravenous databases passed integrity checks.'
        }

        failure {
            publishChecks(
                conclusion: 'FAILURE',
                name: 'Database Integrity',
                summary: 'At least one Ravenous database failed validation.',
                text: 'Check the Jenkins log for database integrity or environment errors.',
                title: 'Database integrity failed'
            )

            echo '❌ Ravenous database integrity CI failed.'
        }

        unstable {
            publishChecks(
                conclusion: 'ACTION_REQUIRED',
                name: 'Database Integrity',
                summary: 'The Ravenous database integrity job is unstable.',
                text: 'Check the Jenkins build log.',
                title: 'Database integrity unstable'
            )

            echo '⚠️ Ravenous database integrity CI is unstable.'
        }

        always {
            sh '''
                echo "Cleaning CI environment..."
                rm -rf .venv
            '''
        }
    }
}