pipeline {
    agent { label 'python' }
    environment {
        PATH="${env.HOME}/.local/bin:${env.PATH}"
        SONAR_HOST_URL="http://192.168.119.2:9000"
        SONAR_PROJECT_KEY="fyre_toolkit"
        SONAR_TOKEN=credentials('sonar')
        UV_PUBLISH_URL="http://192.168.119.2:8082/repository/pypi-internal/"
        UV_PUBLISH_USERNAME="devs"
        UV_PUBLISH_PASSWORD=credentials("uv")
    }
    options {
        skipStagesAfterUnstable()
    }
    stages {
        stage ('Setup') {
            steps {
                script {
                    sh '''
                    printenv
                    uv lock --no-cache
                    uv sync --no-cache --no-editable
                    '''
                }
            }
        }
        stage ('Lint') {
            steps {
                sh 'uv run ruff check'
            }
        }
        stage ('Build') {
            steps {
                sh 'uv build --wheel'

            }
        }
        stage ('Feature Branch Test') {
            when { branch 'PR-*' }
            steps {
                sh '''
                uv run pytest \
                --cov=src \
                --junitxml=pytest_result.xml
                '''
                sh '''
                uv run diff-cover \
                --format html:diff_report.html ./coverage.xml \
                --compare-branch origin/main \
                --ignore-unstaged \
                --ignore-staged \
                --fail-under=80
                '''
            }
        }
        stage ('Main Branch Test') {
            when { branch 'main' }
            steps {
                sh '''
                uv run pytest \
                --cov=src \
                --cov-fail-under=80 \
                --junitxml=pytest_result.xml
                '''
                sh '''
                uv run diff-cover \
                --format html:diff_report.html ./coverage.xml \
                --compare-branch HEAD^ \
                --ignore-unstaged \
                --ignore-staged \
                --fail-under=50
                '''
            }
        }
        stage ('Quality Gate') {
            when { anyOf { branch 'main'; branch 'release-*' } }
            steps {
                withEnv(["SONAR_TOKEN=${SONAR_TOKEN}"]) {
                    echo 'Disable temparily SonarQube analysis for testing...'
                    // sh '''
                    // $SONAR_SCANNER_HOME/bin/sonar-scanner \
                    // -Dsonar.projectKey=${SONAR_PROJECT_KEY} \
                    // -Dsonar.sources=src \
                    // -Dsonar.exclusions=tests/** \
                    // -Dsonar.python.coverage.reportPaths=./coverage.xml \
                    // -Dsonar.host.url=${SONAR_HOST_URL} \
                    // -Dsonar.login=${SONAR_TOKEN}
                    // '''

                    // timeout(time: 1, unit: 'HOURS') {
                    //     waitForQualityGate abortPipeline: true
                    // }
                }
            }
        }
        stage ('Install') {
            when { anyOf { branch 'main'; branch 'release-*' } }
            steps {
                sh 'uv pip install --upgrade dist/fyre_toolkit-0.1.10-py3-none-any.whl'
                sh 'uv run python -c "from testkit import __version__; print(__version__)"'
                sh 'uv pip uninstall fyre-toolkit'
            }
        }
        stage ('Publish') {
            when { anyOf { branch 'main'; branch 'release-*' } }
            steps {
                echo 'Start Publish...'
                // sh 'uv publish --username ${UV_PUBLISH_USERNAME} --password ${UV_PUBLISH_PASSWORD} --publish-url ${UV_PUBLISH_URL}'
            }
        }
    }
    post {
        always {
            archiveArtifacts artifacts: '**/coverage.xml, **/diff_report.html, dist/fyre_toolkit-0.1.0-py3-none-any.whl', allowEmptyArchive: true
            echo "Pipeline completed."
        }
        success {
            echo "Pipeline succeeded."
        }
        failure {
            echo "Pipeline failed. Check logs or SonarQube for details."
        }
    }
}

