pipeline {
    agent { label 'pyDABFActory-CIAgent' }

    parameters {
        booleanParam(name: 'BUILD_ALL_PROFILES', defaultValue: true, description: 'Build all profiles one by one')
        string(name: 'PROFILE', defaultValue: 'demo', description: 'Profile to build when BUILD_ALL_PROFILES is false')
    }

    stages {
        stage('Checkout') {
            steps {
                sh 'rm -Rf live artifacts'
                checkout scm
            }
        }

        stage('Run orchestrator') {
            steps {
                sh '''
                    set -eu
                    CMD="python3 builder/py/orchestrate.py run"
                    if [ "$BUILD_ALL_PROFILES" = "true" ]; then
                        CMD="$CMD --all-profiles"
                    else
                        CMD="$CMD --profile '$PROFILE'"
                    fi
                    eval "$CMD"
                '''
            }
        }
    }

    post {
        always {
            archiveArtifacts artifacts: 'artifacts/**/*', allowEmptyArchive: true
            archiveArtifacts artifacts: 'live/**/*', allowEmptyArchive: true
        }
    }
}
