pipeline {
    agent any
    stages {
        stage('Clone') {
            steps {
                echo '✅ Code cloné depuis Gitea'
            }
        }
        stage('SonarQube Analysis') {
            steps {
                withSonarQubeEnv('sonarqube') {
                    sh '''
                        sonar-scanner \
                          -Dsonar.projectKey=datalab-lite \
                          -Dsonar.projectName="Datalab Lite" \
                          -Dsonar.sources=. \
                          -Dsonar.inclusions="**/*.py" \
                          -Dsonar.python.version=3.11
                    '''
                }
            }
        }
        stage('Quality Gate') {
            steps {
                waitForQualityGate abortPipeline: false
            }
        }
        stage('Deploy') {
            steps {
                sh '''
                    # Mettre à jour le ConfigMap code
                    kubectl create configmap datalab-lite-code \
                        --from-file=main.py=main.py \
                        --from-file=k8s.py=k8s.py \
                        --from-file=minio_client.py=minio_client.py \
                        -n datalab-lite \
                        --dry-run=client -o yaml | kubectl apply -f -

                    # Mettre à jour le ConfigMap templates
                    kubectl create configmap datalab-lite-templates \
                        --from-file=index.html=templates/index.html \
                        -n datalab-lite \
                        --dry-run=client -o yaml | kubectl apply -f -

                    # Redémarrer le pod
                    kubectl rollout restart deployment datalab-lite -n datalab-lite

                    echo "✅ Datalab Lite déployé"
                '''
            }
        }
    }
    post {
        success { echo '✅ Pipeline terminé avec succès !' }
        failure { echo '❌ Pipeline échoué !' }
    }
}
