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

pipeline {
    agent { label 'rust_no_docker' }

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

                sh '''
                    set -e

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

        stage('Install Typst') {
            steps {
                sh '''
                    set -e

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

                    if ! command -v typst >/dev/null 2>&1; then
                        echo "Typst not found — installing it..."
                        cargo binstall --no-confirm typst-cli
                    else
                        echo "Typst already installed."
                    fi

                    echo "Using:"
                    typst --version
                '''
            }
        }

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

                    mkdir -p .ci-build

                    # Top-level .typ files are considered document targets.
                    # Files below data/, etc. are support modules and are not
                    # expected to compile as standalone documents.
                    TARGETS="$(git ls-files '*.typ' | awk '!/\\//')"

                    if [ -z "${TARGETS}" ]; then
                        echo "ERROR: no Typst document targets found." >&2
                        exit 1
                    fi

                    echo "Typst targets:"
                    printf '%s\\n' "${TARGETS}" | sed 's/^/  - /'
                    echo

                    failed=0

                    for source in ${TARGETS}; do
                        name="$(basename "${source}" .typ)"
                        output=".ci-build/${name}.pdf"

                        echo "============================================================"
                        echo "Compiling ${source}"
                        echo "============================================================"

                        if typst compile "${source}" "${output}"; then
                            echo "✓ ${source}"
                        else
                            echo "✗ ${source}" >&2
                            failed=1
                        fi

                        echo
                    done

                    if [ "${failed}" -ne 0 ]; then
                        echo "One or more Typst targets failed to compile." >&2
                        exit 1
                    fi

                    echo "All Typst targets compiled successfully."
                '''
            }
        }
    }

    post {
        success {
            publishChecks(
                conclusion: 'SUCCESS',
                name: 'Typst CI',
                summary: 'All YonSen Typst targets compiled successfully.',
                text: 'All standalone Typst document targets passed compilation.',
                title: 'Typst compilation successful'
            )

            echo '✅ All YonSen Typst targets compile successfully.'
        }

        failure {
            publishChecks(
                conclusion: 'FAILURE',
                name: 'Typst CI',
                summary: 'At least one YonSen Typst target failed to compile.',
                text: 'Check the Jenkins compilation log for the Typst error.',
                title: 'Typst compilation failed'
            )

            echo '❌ A YonSen Typst target failed to compile.'
        }

        unstable {
            publishChecks(
                conclusion: 'ACTION_REQUIRED',
                name: 'Typst CI',
                summary: 'The YonSen Typst CI completed with an unstable status.',
                text: 'Check the Jenkins build log.',
                title: 'Typst CI unstable'
            )

            echo '⚠️ YonSen Typst CI is unstable.'
        }

        always {
            sh '''
                rm -rf .ci-build
            '''
        }
    }
}