// Settings
@Library('jenkins-devops-cicd-library') _
import com.bnpparibas.release.*

// Offline Windows Python runtime installer. FQDN-targeted, no inventory.
// Shared creds + ansible run live in projects_deploy/_ansible.groovy.
// Payload branch ships: python-windows.zip (runtime) + wheels-windows.zip.

def payloadDir = 'python_payload'

pipeline {
    agent { label 'Bnpp-Ansible' }

    parameters {
        choice(name: 'Environment', choices: ['DEV', 'SIT', 'STG', 'PRD'],
               description: 'Selects the CyberArk safe / superuser.')
        string(name: 'TargetHosts', defaultValue: '',
               description: 'Target FQDN(s), comma-separated, e.g. eurv0ii40617.xmp.net.intra')
        string(name: 'WheelsRepoUrl', defaultValue: '',
               description: 'Git URL of the repo holding the payload branch.')
        string(name: 'WheelsBranch', defaultValue: '',
               description: 'Branch with python-windows.zip and wheels-windows.zip.')
        string(name: 'InstallDir', defaultValue: 'D:\\Apps\\python',
               description: 'Absolute target directory for the Python runtime.')
        string(name: 'PythonExe', defaultValue: 'python.exe',
               description: 'Interpreter path relative to InstallDir.')
        string(name: 'PostInstallCommand', defaultValue: '',
               description: 'Optional command to run after install (cwd = InstallDir). Blank to skip.')
        choice(name: 'RunOnAllNodes', choices: ['yes', 'no'],
               description: 'yes => every host / no => run once.')
        choice(name: 'ElevatePrivileges', choices: ['yes', 'no'],
               description: 'yes => pip-install as the service account via CyberArk runas.')
    }

    environment {
        GITCREDENTIALS = 'd84907'
    }

    stages {
        stage('Validate') {
            steps {
                script {
                    if (!params.TargetHosts?.trim()) { error('TargetHosts is required.') }
                    if (!params.WheelsRepoUrl?.trim() || !params.WheelsBranch?.trim()) {
                        error('WheelsRepoUrl and WheelsBranch are required.')
                    }
                }
            }
        }

        stage('Checkout payload') {
            steps {
                dir("${payloadDir}") {
                    git(url: "${params.WheelsRepoUrl}", branch: "${params.WheelsBranch}",
                        credentialsId: "${env.GITCREDENTIALS}")
                }
            }
        }

        stage('Install Python') {
            steps {
                script {
                    def ansible = load 'deployment/projects_deploy/_ansible.groovy'
                    ansible.run(
                        environment: params.Environment,
                        os:          'windows',
                        playbook:    'projects_deploy/install_python_win.yml',
                        hosts:       params.TargetHosts,
                        extraVars: [
                            WORKSPACE:          env.WORKSPACE,
                            PayloadDir:         payloadDir,
                            InstallDir:         params.InstallDir,
                            PythonExe:          params.PythonExe,
                            PostInstallCommand: params.PostInstallCommand,
                            RunOnAllNodes:      params.RunOnAllNodes,
                            ElevatePrivileges:  params.ElevatePrivileges
                        ]
                    )
                }
            }
        }
    }
}
