// Add this stage to your existing Jenkinsfile
// Requires: Node.js tool configured, Chrome/Chromium installed on agent

pipeline {
    agent { label 'linux' }

    environment {
        RF_LICENSE_KEY = credentials('reportforge-license-key')
    }

    stages {
        stage('Install') {
            steps {
                sh 'npm ci'
                sh 'npx playwright install chromium'
            }
        }

        stage('Playwright Tests + PDF Report') {
            steps {
                sh 'npx playwright test'
            }
            post {
                always {
                    // Archive the PDF report as a build artifact
                    archiveArtifacts(
                        artifacts: 'playwright-report/*.pdf',
                        allowEmptyArchive: true,
                        fingerprint: true
                    )
                    // Publish HTML alongside (optional)
                    publishHTML(target: [
                        allowMissing: true,
                        alwaysLinkToLastBuild: true,
                        keepAll: true,
                        reportDir: 'playwright-report',
                        reportFiles: '*.pdf',
                        reportName: 'Playwright PDF Report'
                    ])
                }
            }
        }
    }

    post {
        failure {
            emailext(
                subject: "FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
                body: "Build failed. PDF report: ${env.BUILD_URL}artifact/playwright-report/",
                recipientProviders: [[$class: 'DevelopersRecipientProvider']]
            )
        }
    }
}
