pipeline {
    agent {
        label 'master'
    }

    environment {
        NODE_ENV = 'production'
        NEXT_TELEMETRY_DISABLED = '1'
        npm_config_registry = 'https://registry.npmmirror.com'
        PROJECT_DIR = '/home/novalon/docker-app/novalon-website'
        DEPLOY_DIR = '/home/novalon/docker-app/novalon-website'
    }

    stages {
        stage('Checkout') {
            steps {
                echo '=== Checking out code from Gitea ==='
                checkout scm
                sh '''
                    echo "Current branch: ${BRANCH_NAME}"
                    echo "Commit: ${GIT_COMMIT}"
                    echo "Workspace: ${WORKSPACE}"
                '''
            }
        }

        stage('Install Dependencies') {
            steps {
                echo '=== Installing dependencies ==='
                sh '''
                    cd ${PROJECT_DIR}
                    npm ci --cache /tmp/npm-cache --prefer-offline --legacy-peer-deps || npm ci --cache /tmp/npm-cache --legacy-peer-deps
                '''
            }
        }

        stage('Code Quality Check') {
            parallel {
                stage('Lint') {
                    steps {
                        echo '=== Running linting ==='
                        sh '''
                            cd ${PROJECT_DIR}
                            npm run lint
                        '''
                    }
                }

                stage('Type Check') {
                    steps {
                        echo '=== Running type check ==='
                        sh '''
                            cd ${PROJECT_DIR}
                            npm run type-check
                        '''
                    }
                }

                stage('Security Scan') {
                    steps {
                        echo '=== Running security scan ==='
                        sh '''
                            cd ${PROJECT_DIR}
                            npm audit --audit-level=high --omit=dev || true
                        '''
                    }
                }
            }
        }

        stage('Unit Tests') {
            when {
                branch 'dev'
            }
            steps {
                echo '=== Running unit tests ==='
                sh '''
                    cd ${PROJECT_DIR}
                    npm run test:unit -- --coverage --coverageReporters=text-summary --forceExit || true
                    echo "Unit tests completed."
                '''
            }
        }

        stage('E2E Tests') {
            when {
                branch 'dev'
            }
            steps {
                echo '=== Running E2E tests ==='
                sh '''
                    cd ${PROJECT_DIR}
                    npm run build
                    npx playwright install chromium --with-deps || true
                    npm run test:e2e || true
                '''
            }
        }

        stage('Build and Deploy') {
            when {
                anyOf {
                    branch 'release'
                    branch pattern: 'release/**', comparator: 'GLOB'
                }
            }
            steps {
                echo '=== Building and deploying to production ==='
                sh '''
                    echo "Current container info:"
                    echo "Hostname: $(hostname)"
                    echo "IP: $(hostname -i)"
                    echo ""

                    cd ${PROJECT_DIR}
                    echo "Building production artifacts..."
                    npm run build

                    echo "Build completed"
                    ls -la dist/

                    echo "Deploying to production..."
                    chmod +x scripts/sync-to-production.sh
                    ./scripts/sync-to-production.sh

                    echo "Restarting production services..."
                    cd ${DEPLOY_DIR}
                    test -f docker-compose.server.yml && mv docker-compose.server.yml docker-compose.yml
                    chmod +x scripts/deploy-production.sh
                    ./scripts/deploy-production.sh

                    echo "Production deployment completed"
                '''
            }
        }

        stage('Archive to Main') {
            when {
                anyOf {
                    branch 'release'
                    branch pattern: 'release/**', comparator: 'GLOB'
                }
            }
            steps {
                echo '=== Archiving to main branch ==='
                sh '''
                    cd ${PROJECT_DIR}
                    chmod +x scripts/archive-to-main.sh
                    ./scripts/archive-to-main.sh
                '''
            }
        }
    }

    post {
        success {
            echo '=== Build succeeded! ==='
            script {
                if (env.BRANCH_NAME.startsWith('release')) {
                    echo 'Sending success notification to WeChat...'
                }
            }
        }

        failure {
            echo '=== Build failed! ==='
            script {
                if (env.BRANCH_NAME.startsWith('release')) {
                    echo 'Sending failure notification to WeChat...'
                }
            }
        }

        always {
            echo '=== Cleaning up workspace ==='
            cleanWs()
        }
    }
}
