pipeline {
    agent { label 'rust_no_docker' }

    options {
        disableConcurrentBuilds()
        skipDefaultCheckout(true)
    }

    environment {
        CI_REPORT_DIR = '.ci-publication'
        CI_ARTIFACT_DIR = '.ci-artifacts'
        DEPLOY_DIR = '/mnt/books/work_reports'
        DIAGNOSTICS_AUTHOR = 'Beffroi'
    }

    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
                ])
            }
        }

        stage('Test Components and Integration') {
            steps {
                sh './beffroi.sh test'
            }
        }

        stage('Build Reports') {
            steps {
                sh '''
                    set -eu
                    rm -rf "${CI_ARTIFACT_DIR}"
                    ./beffroi.sh all "${CI_REPORT_DIR}" --replace
                '''
            }
        }

        stage('Package Reports') {
            environment {
                REPORT_ZIP_PASSWORD = credentials('typst-reporter-publish-password')
            }
            steps {
                sh '''
                    set +x
                    set -eu
                    ./beffroi.sh package "${CI_REPORT_DIR}" "${CI_ARTIFACT_DIR}"
                '''
                archiveArtifacts artifacts: '.ci-artifacts/*.zip', fingerprint: true
            }
        }

        stage('Publish Reports') {
            when { branch 'main' }
            steps {
                sh '''
                    set -eu
                    mkdir -p "${DEPLOY_DIR}"
                    for archive in Reports.zip Diagnostics.zip; do
                        cp "${CI_ARTIFACT_DIR}/${archive}" "${DEPLOY_DIR}/.${archive}.tmp"
                        mv -f "${DEPLOY_DIR}/.${archive}.tmp" "${DEPLOY_DIR}/${archive}"
                    done
                '''
            }
        }
    }

    post {
        success {
            publishChecks(
                conclusion: 'SUCCESS',
                name: 'Beffroi Reporting CI',
                summary: 'All checks passed and encrypted report archives were built.',
                text: 'Validation, rendering, encrypted packaging, and main-branch publication completed successfully.',
                title: 'Beffroi reports published'
            )
            echo '✅ Beffroi Reporting CI passed.'
        }
        failure {
            publishChecks(
                conclusion: 'FAILURE',
                name: 'Beffroi Reporting CI',
                summary: 'A component or integration check failed.',
                text: 'Review the Jenkins log for the failing repository, contract, publication, or rendering check.',
                title: 'Beffroi Reporting CI failed'
            )
            echo '❌ Beffroi Reporting CI failed.'
        }
        unstable {
            publishChecks(
                conclusion: 'ACTION_REQUIRED',
                name: 'Beffroi Reporting CI',
                summary: 'The integration build is unstable.',
                text: 'Review the Jenkins log for unstable checks.',
                title: 'Beffroi Reporting CI unstable'
            )
            echo '⚠️ Beffroi Reporting CI is unstable.'
        }
    }
}
