// Add the Gitea webhook configured for this Jenkins installation.

pipeline {
    agent { label 'rust_no_docker' }

    options {
        disableConcurrentBuilds()
        skipDefaultCheckout(true)
    }

    environment {
        PYTHONUNBUFFERED = '1'
        UV_CACHE_DIR = "${WORKSPACE}/.uv-cache"
        UV_PYTHON_INSTALL_DIR = "${WORKSPACE}/.uv-python"
    }

    stages {
        stage('Checkout') {
            steps {
                deleteDir()
                checkout scm
                sh 'git fetch --force --tags origin'
            }
        }

        stage('Set up development environment') {
            steps {
                sh '''
                    set -eu
                    command -v uv >/dev/null 2>&1 || {
                        echo "ERROR: uv is required on the Jenkins worker." >&2
                        exit 1
                    }
                    uv --version
                    command -v python3
                    python3 --version
                    uv venv --python python3 .venv
                    uv pip install --python .venv/bin/python -e '.[tests,dev]'
                '''
            }
        }

        stage('Style') {
            steps {
                sh 'uv run --no-sync pre-commit run --all-files'
            }
        }

        stage('Tests') {
            steps {
                sh 'uv run --no-sync python -m unittest'
            }
        }

        stage('Build package') {
            steps {
                sh './build-package.sh'
            }
        }

        stage('Wheel install with pip') {
            steps {
                sh '''
                    set -eu
                    wheel="$(find dist -maxdepth 1 -type f -name 'vsg-*.whl' -print -quit)"
                    test -n "${wheel}" || {
                        echo "ERROR: built wheel was not found." >&2
                        exit 1
                    }

                    python3 -m venv .ci-pip-venv
                    .ci-pip-venv/bin/python -m pip install "${wheel}"

                    output="$(
                        printf 'entity smoke is\nend entity smoke;\n' |
                            .ci-pip-venv/bin/vsg --stdin --stdout --fix --style jcl --no-failure
                    )"
                    printf '%s\n' "${output}" | grep -F 'entity SMOKE is'
                '''
            }
        }

        stage('Wheel install with uv') {
            steps {
                sh '''
                    set -eu
                    wheel="$(find dist -maxdepth 1 -type f -name 'vsg-*.whl' -print -quit)"
                    test -n "${wheel}" || {
                        echo "ERROR: built wheel was not found." >&2
                        exit 1
                    }

                    uv venv --python .venv/bin/python .ci-uv-venv
                    uv pip install --python .ci-uv-venv/bin/python "${wheel}"

                    output="$(
                        printf 'entity smoke is\nend entity smoke;\n' |
                            .ci-uv-venv/bin/vsg --stdin --stdout --fix --style jcl --no-failure
                    )"
                    printf '%s\n' "${output}" | grep -F 'entity SMOKE is'
                '''
            }
        }

        stage('Archive package') {
            steps {
                archiveArtifacts artifacts: 'dist/*', fingerprint: true, onlyIfSuccessful: true
            }
        }

        stage('Publish package') {
            when {
                branch 'master'
            }
            steps {
                withCredentials([
                    string(
                        credentialsId: 'vsg-gitea-package-token',
                        variable: 'GITEA_TOKEN'
                    )
                ]) {
                    sh '''
                        set +x
                        set -eu

                        test -n "${GITEA_TOKEN}" || {
                            echo "ERROR: Jenkins supplied an empty Gitea package token." >&2
                            exit 1
                        }

                        for artifact in dist/vsg-*.tar.gz dist/vsg-*.whl; do
                            test -f "${artifact}" || {
                                echo "ERROR: expected package artifact was not found: ${artifact}" >&2
                                exit 1
                            }
                        done

                        UV_PUBLISH_USERNAME='Dandaedre' \
                            UV_PUBLISH_PASSWORD="${GITEA_TOKEN}" \
                            uv publish \
                                --publish-url 'https://proxy.dandaedre.com/gitea/api/packages/MadGobblins/pypi' \
                                --check-url 'https://proxy.dandaedre.com/gitea/api/packages/MadGobblins/pypi/simple/' \
                                --no-attestations \
                                dist/vsg-*.tar.gz \
                                dist/vsg-*.whl
                    '''
                }
            }
        }
    }

    post {
        success {
            publishChecks(
                conclusion: 'SUCCESS',
                name: 'VSG fork CI',
                summary: 'VSG tests and package installation checks passed.',
                text: 'The wheel was tested through both pip and uv, including stdin/stdout formatting.',
                title: 'Build Successful'
            )
        }
        failure {
            publishChecks(
                conclusion: 'FAILURE',
                name: 'VSG fork CI',
                summary: 'A VSG fork check failed.',
                text: 'Review the Jenkins log for style, tests, build, installation, or publishing failures.',
                title: 'Build Failed'
            )
        }
    }
}
