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

pipeline {
    agent { label 'rust_no_docker' }

    options { disableConcurrentBuilds() }

    environment {
        CARGO_TERM_COLOR = 'always'

        CI_ARTIFACT_DIR = '.ci-artifacts'
    }

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

        stage('Validate Repository') {
            steps {
                sh '''
                    set -eu

                    echo "Validating Win Diagnostics repository..."

                    test -f README.md
                    test -f LICENSE
                    test -f AGENT.md

                    echo "Repository structure OK."
                '''
            }
        }

        /*
         * Windows collection is intentionally NOT run here.
         *
         * This Jenkins agent is not the target Windows machine.
         *
         * Collection scripts should eventually receive separate
         * Windows-specific validation if a suitable CI worker becomes
         * available.
         */

        stage('Validate Contract') {
            steps {
                sh '''
                    set -eu
                    command -v uv >/dev/null 2>&1 || { echo "uv is required for the isolated validation environment" >&2; exit 1; }
                    command -v jq >/dev/null 2>&1 || { echo "jq is required" >&2; exit 1; }
                    make test
                '''
            }
        }

        /*
         * TODO: Analysis Tests
         *
         * Once sanitized diagnostic fixtures exist:
         *
         *     fixtures/
         *
         * run the analysis engine against them.
         *
         * This should be platform-independent wherever practical.
         */

        /*
         * TODO: Regression Tests
         *
         * Known diagnostic inputs should produce expected findings,
         * severity and evidence relationships.
         */

        /*
         * No rendering or publishing stages belong here.
         *
         * Win Diagnostics produces structured diagnostic information.
         * It does not own reports or PDFs.
         */
    }

    post {
        success {
            publishChecks(
                conclusion: 'SUCCESS',
                name: 'Win Diagnostics CI',
                summary: 'Win Diagnostics passed all currently implemented CI checks.',
                text: 'Repository, diagnostic contract, and first offline-analysis fixture tests passed. Native collection and remaining rules remain pending.',
                title: 'Win Diagnostics CI passed'
            )

            echo '✅ Win Diagnostics CI passed.'
        }

        failure {
            publishChecks(
                conclusion: 'FAILURE',
                name: 'Win Diagnostics CI',
                summary: 'Win Diagnostics failed CI.',
                text: 'A repository or validation check failed. Check the Jenkins log.',
                title: 'Win Diagnostics CI failed'
            )

            echo '❌ Win Diagnostics CI failed.'
        }

        unstable {
            publishChecks(
                conclusion: 'ACTION_REQUIRED',
                name: 'Win Diagnostics CI',
                summary: 'Win Diagnostics CI is unstable.',
                text: 'The Jenkins build completed with an unstable status.',
                title: 'Win Diagnostics CI unstable'
            )

            echo '⚠️ Win Diagnostics CI is unstable.'
        }

        always {
            echo 'CI finished.'
        }
    }
}
