pipeline {

    environment {
        PY_COLORS = "0"
    }

    agent any

    stages {
        stage('CI') {
            matrix {
                axes {
                    axis {
                        name 'PYTHON_VERSION'
                        values '3.11', '3.12', '3.13', '3.14', '3.15'
                    }
                }
                stages {
                    stage('Python Build') {
                        options {
                            lock('single-pytest-csv-params-build')
                        }
                        stages {
                            stage('Prepare') {
                                steps {
                                    sh 'uv sync --python ${PYTHON_VERSION} --group dev'
                                    sh 'uv run  --python ${PYTHON_VERSION} coverage erase'
                                }
                            }
                            stage('Test') {
                                steps {
                                    sh 'uv run --python ${PYTHON_VERSION} pytest --cache-clear --cov-append'
                                    xunit checksName: "Tests ${PYTHON_VERSION}", tools: [JUnit(excludesPattern: '', pattern: 'test-reports/*.xml', stopProcessingIfError: true)]
                                    recordCoverage(
                                        tools: [[parser: 'COBERTURA', pattern: 'coverage.xml']],
                                        id: "python-${PYTHON_VERSION}-coverage",
                                        name: "Python ${PYTHON_VERSION} Coverage",
                                        sourceCodeRetention: 'EVERY_BUILD',
                                        qualityGates: [
                                            [threshold: 80.0, metric: 'LINE', baseline: 'PROJECT', unstable: true],
                                            [threshold: 70.0, metric: 'BRANCH', baseline: 'PROJECT', unstable: true]
                                        ]
                                    )
                                }
                            }
                            stage('Build') {
                                steps {
                                    sh 'uv build'
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    post {
        success {
            deleteDir()
        }
        always {
            withCredentials([string(credentialsId: 'jed-notification-email', variable: 'EMAIL')]) {
                mail to: '$EMAIL',
                     subject: "[${currentBuild.fullDisplayName}] ${currentBuild.currentResult}",
                     body: "Duration: ${currentBuild.durationString} / Jenkins URL: ${env.BUILD_URL}"
            }
        }
    }

}
