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

pipeline {
    agent { label 'rust_no_docker' }

    options {
        disableConcurrentBuilds()
        skipDefaultCheckout(true)
    }

    environment {
        CARGO_TERM_COLOR = 'always'
        CI_ARTIFACT_DIR = '.ci-artifacts'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: scm.branches,
                    extensions: [
                        [$class: 'SubmoduleOption',
                            disableSubmodules: false,
                            parentCredentials: true,
                            recursiveSubmodules: true,
                            reference: '',
                            shallow: false,
                            trackingSubmodules: false
                        ]
                    ],
                    userRemoteConfigs: scm.userRemoteConfigs
                ])

                sh '''
                    set -eu

                    echo
                    echo "Submodules:"
                    git submodule status --recursive
                '''
            }
        }
        stage('Validate Workspace') {
            steps {
                sh '''
                    set -eu

                    echo "Validating macro repository..."

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

                    echo
                    echo "Submodules:"
                    git submodule status --recursive

                    echo
                    echo "Workspace structure OK."
                '''
            }
        }


        /*
         * Legacy references
         * -----------------
         *
         * During migration, references/WinDiagnostics and
         * references/typst_reporter may also be submodules.
         *
         * They are migration references and should NOT become runtime
         * dependencies of the final architecture.
         *
         * We therefore intentionally do not require them here forever.
         */

        stage('Component Contract Checks') {
            steps {
                sh 'bash scripts/check-components.sh'
            }
        }

        /*
         * TODO: Integration Test
         *
         * The current synthetic check covers raw-capture analysis, adapter
         * generation and PDF rendering using the first migrated diagnostic rule.
         * Expand it as the remaining rule catalog and collector are implemented:
         *
         *     diagnostic fixture
         *          ↓
         *     win-diagnostics
         *          ↓
         *     diagnostic.json
         *          ↓
         *     beffroi-reports adapter
         *          ↓
         *     report.yaml
         *          ↓
         *     reportist
         *          ↓
         *     report.pdf
         */

        /* Typst is an explicit agent dependency checked by make check. */

        /*
         * TODO: Setup Packaging
         *
         * The previous reporting project used:
         *
         *     uv venv --allow-existing .ci-venv
         *     uv pip install ...
         *
         * Do not migrate this until packaging is actually needed.
         */

        /*
         * TODO: Render Reports
         *
         * Rendering should use Reportist against report definitions owned
         * by beffroi-reports.
         */

        /*
         * TODO: Package Reports
         *
         * If encrypted report archives remain useful, migrate the existing
         * pyzipper-based packaging here after the new pipeline works.
         */

        /*
         * TODO: Publish Reports
         *
         * Potential future destination:
         *
         *     /mnt/books/Reports
         *
         * Do not enable publication until generated output has been
         * manually compared with the existing production reports.
         */
    }

    post {
        success {
            publishChecks(
                conclusion: 'SUCCESS',
                name: 'Beffroi Reporting CI',
                summary: 'Beffroi Reporting workspace passed all currently implemented CI checks.',
                text: 'Contracts and the first raw-capture analysis-to-PDF integration passed. Remaining diagnostic rules and native collection are pending.',
                title: 'Reporting workspace healthy'
            )

            echo '✅ Beffroi Reporting workspace is healthy.'
        }

        failure {
            publishChecks(
                conclusion: 'FAILURE',
                name: 'Beffroi Reporting CI',
                summary: 'Beffroi Reporting integration CI failed.',
                text: 'Workspace checkout, submodule initialization, or validation failed. Check the Jenkins log.',
                title: 'Reporting workspace failed'
            )

            echo '❌ Beffroi Reporting integration CI failed.'
        }

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

            echo '⚠️ Beffroi Reporting integration CI is unstable.'
        }

        always {
            echo 'CI finished.'
        }
    }
}
