pipeline {
    agent any
    
    tools {
        jdk 'JDK17'
        maven 'Maven3'
    }
    
    environment {
        HEADLESS = 'true'
    }
    
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
        
        stage('Build') {
            steps {
                sh 'mvn clean compile'
            }
        }
        
        stage('Test') {
            steps {
                sh 'mvn test -Dheadless=${HEADLESS}'
            }
            post {
                always {
                    testResultsPublisher(testResultsPattern: '**/target/surefire-reports/*.xml')
                }
            }
        }
    }
    
    post {
        failure {
            archiveArtifacts artifacts: 'screenshots/**', allowEmptyArchive: true
        }
        always {
            cleanWs()
        }
    }
}