pipeline {
    agent any

    environment {
        NODE_VERSION = '20'
        PROJECT_DIR = 'everything-is-suitable-uniapp'
    }

    stages {
        stage('Install Dependencies') {
            steps {
                dir(env.PROJECT_DIR) {
                    sh 'npm ci'
                }
            }
        }

        stage('Lint') {
            steps {
                dir(env.PROJECT_DIR) {
                    sh 'npx eslint src/ --ext .ts,.vue --max-warnings 0 || true'
                }
            }
        }

        stage('Type Check') {
            steps {
                dir(env.PROJECT_DIR) {
                    sh 'npx tsc --noEmit || true'
                }
            }
        }

        stage('Unit Tests') {
            steps {
                dir(env.PROJECT_DIR) {
                    sh 'npx vitest run --coverage'
                }
            }
            post {
                always {
                    dir(env.PROJECT_DIR) {
                        junit allowEmptyResults: true, testResults: 'coverage/junit.xml'
                        publishHTML(target: [
                            reportDir: 'coverage',
                            reportFiles: 'index.html',
                            reportName: 'Coverage Report'
                        ])
                    }
                }
            }
        }

        stage('Quality Gate') {
            steps {
                script {
                    def coverageResult = sh(
                        script: "cd ${env.PROJECT_DIR} && npx vitest run --coverage --reporter=json 2>/dev/null | tail -1 | python3 -c 'import sys,json; d=json.load(sys.stdin); print(d.get(\"coverageMap\",{}).get(\"total\",{}).get(\"lines\",{}).get(\"pct\",0))' 2>/dev/null || echo '0'",
                        returnStdout: true
                    ).trim()
                    def coverage = coverageResult as double
                    if (coverage < 80.0) {
                        error "Coverage ${coverage}% is below 80% threshold"
                    }
                    echo "Coverage: ${coverage}% - PASSED"
                }
            }
        }

        stage('Build H5') {
            steps {
                dir(env.PROJECT_DIR) {
                    sh 'npm run build:h5'
                }
            }
        }

        stage('Build Android') {
            when {
                branch 'main'
            }
            steps {
                dir(env.PROJECT_DIR) {
                    sh 'npm run build:app-android || echo "Android build requires HBuilderX CLI"'
                }
            }
        }

        stage('Build iOS') {
            when {
                branch 'main'
            }
            steps {
                dir(env.PROJECT_DIR) {
                    sh 'npm run build:app-ios || echo "iOS build requires HBuilderX CLI"'
                }
            }
        }

        stage('E2E Tests') {
            steps {
                dir(env.PROJECT_DIR) {
                    sh 'npx playwright install --with-deps chromium'
                    sh 'npx playwright test || true'
                }
            }
            post {
                always {
                    dir(env.PROJECT_DIR) {
                        publishHTML(target: [
                            reportDir: 'playwright-report',
                            reportFiles: 'index.html',
                            reportName: 'E2E Report'
                        ])
                    }
                }
            }
        }
    }

    post {
        always {
            dir(env.PROJECT_DIR) {
                cleanWs()
            }
        }
        success {
            echo 'Pipeline completed successfully'
        }
        failure {
            echo 'Pipeline failed - check stage logs for details'
        }
    }
}
