pipeline {
    agent { label 'vm-dev-docker' }

    environment {
        DEVPI_URL   = 'https://devpi.dattos.com.br'
        DEVPI_INDEX = 'root/dattos'
        VENV_DIR    = '.venv'
        ACTIVATE    = '. .venv/bin/activate'
    }

    stages {
        stage('Setup Python') {
            steps {
                sh '''
                    python3 -m venv ${VENV_DIR}
                    ${ACTIVATE}
                    pip install --upgrade pip
                    pip install build devpi-client
                    pip install -e ".[dev,mcp,http]"
                '''
            }
        }

        stage('Test') {
            steps {
                sh '''
                    ${ACTIVATE}
                    pytest -v
                '''
            }
        }

        stage('Build wheel/sdist') {
            when {
                anyOf {
                    branch 'hotfix'
                    branch 'develop'
                    branch 'release-fix'
                    tag 'v*'
                    tag 'r*'
                }
            }
            steps {
                sh '''
                    ${ACTIVATE}
                    rm -rf dist/
                    python -m build
                    ls -la dist/
                '''
            }
        }

        stage('Publish to devpi (dev branches)') {
            when {
                anyOf {
                    branch 'hotfix'
                    branch 'develop'
                    branch 'release-fix'
                }
            }
            steps {
                script {
                    def guardExit = sh(
                        script: '.venv/bin/python scripts/check_dev_version.py pyproject.toml',
                        returnStatus: true
                    )
                    if (guardExit != 0) {
                        echo 'Version guard: not a .devN release — skipping devpi publish.'
                        return
                    }
                    withCredentials([string(credentialsId: 'DEVPI_PASS', variable: 'DEVPI_PASS')]) {
                        // Feed password via stdin to avoid exposing it on the process command line.
                        // Matches the credential and pattern used by dattos-common-lib-py.
                        sh '''
                            ${ACTIVATE}
                            devpi use ${DEVPI_URL}/${DEVPI_INDEX}
                            printf '%s\n' "$DEVPI_PASS" | devpi login root
                            devpi upload --from-dir dist/
                            devpi logoff
                        '''
                    }
                }
            }
        }

        stage('Publish to devpi (tag)') {
            when {
                anyOf {
                    tag 'v*'
                    tag 'r*'
                }
            }
            steps {
                withCredentials([string(credentialsId: 'DEVPI_PASS', variable: 'DEVPI_PASS')]) {
                    sh '''
                        ${ACTIVATE}
                        devpi use ${DEVPI_URL}/${DEVPI_INDEX}
                        printf '%s\n' "$DEVPI_PASS" | devpi login root
                        devpi upload --from-dir dist/
                        devpi logoff
                    '''
                }
            }
        }
    }

    post {
        always {
            sh 'rm -rf dist/ build/ src/*.egg-info/ .pytest_cache/ ${VENV_DIR} || true'
            echo 'Pipeline finished.'
        }
        success {
            echo 'Build successful.'
        }
        failure {
            echo 'Pipeline failed.'
        }
    }
}
