pipeline {
    agent any
    
    parameters {
        choice(name: 'BROWSER', choices: ['chrome', 'firefox'], description: 'Browser to run tests')
        choice(name: 'TEST_ENV', choices: ['dev', 'qa', 'prod'], description: 'Test environment')
    }
    
    environment {
        PYTHON_VERSION = '3.8+'
        HEADLESS = 'true'
    }
    
    stages {
        stage('Setup') {
            steps {
                script {
                    echo "Setting up Python ${PYTHON_VERSION}"
                    sh '''
                        python${PYTHON_VERSION} -m venv venv
                        . venv/bin/activate
                        python -m pip install --upgrade pip
                        pip install -r requirements.txt
                    '''
                }
            }
        }
        
        stage('Run Tests') {
            steps {
                script {
                    sh '''
                        . venv/bin/activate
                        pytest -v -n auto
                    '''
                }
            }
        }
        
    }
    
    post {
        always {
            publishHTML([
                allowMissing: false,
                alwaysLinkToLastBuild: true,
                keepAll: true,
                reportDir: 'reports',
                reportFiles: 'report.html',
                reportName: 'Pytest HTML Report'
            ])
            archiveArtifacts artifacts: 'screenshots/**', allowEmptyArchive: true
            archiveArtifacts artifacts: 'logs/**', allowEmptyArchive: true
        }
        
        success {
            echo 'Tests passed successfully!'
        }
        
        failure {
            echo 'Tests failed. Check the artifacts for details.'
        }
    }
}
