pipeline {
    agent any

    options {
        disableConcurrentBuilds()
    }

    environment {
        DEPLOY_USER = 'hskwon'
        RELEASE_ID  = new Date().format('yyyyMMdd_HHmmss')
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
                script {
                    env.GIT_COMMIT_MSG = sh(script: "git log -1 --pretty=format:'%s'", returnStdout: true).trim()
                }
                slackSend channel: '#product_infra', color: '#439FE0', tokenCredentialId: 'slack-token',
                    message: "🚀 *react* 빌드 시작 (`${env.BRANCH_NAME}`)\n${env.GIT_COMMIT_MSG}\n<${env.BUILD_URL}|빌드 #${env.BUILD_NUMBER}>"
            }
        }

        stage('Prepare Env') {
            steps {
                script {
                    if (env.BRANCH_NAME == 'main') {
                        // main: Stage 빌드 먼저 → Production 재빌드
                        sh "cp /var/lib/jenkins/env-files/react/.env.stage .env.production"
                    } else {
                        def envFile = "/var/lib/jenkins/env-files/react/.env.${env.BRANCH_NAME}"
                        sh "cp ${envFile} .env.production"
                    }
                }
            }
        }

        stage('Install') {
            steps { sh 'npm install --prefer-offline' }
        }

        stage('Build') {
            steps { sh 'npm run build' }
        }

        // ── develop → 개발서버 배포 ──
        stage('Deploy Development') {
            when { branch 'develop' }
            steps {
                sshagent(credentials: ['deploy-ssh-key']) {
                    sh """
                        rsync -az --delete \
                            --exclude='.git' --exclude='.env*' --exclude='ecosystem.config.*' \
                            .next package.json next.config.ts public node_modules \
                            ${DEPLOY_USER}@114.203.209.83:/home/webservice/react/
                        scp .env.production ${DEPLOY_USER}@114.203.209.83:/home/webservice/react/.env.production
                        ssh ${DEPLOY_USER}@114.203.209.83 'cd /home/webservice/react && pm2 restart sam-react'
                    """
                }
            }
        }

        // ── main → 운영서버 Stage 배포 ──
        stage('Deploy Stage') {
            when { branch 'main' }
            steps {
                sshagent(credentials: ['deploy-ssh-key']) {
                    sh """
                        ssh ${DEPLOY_USER}@211.117.60.189 'mkdir -p /home/webservice/react-stage/releases/${RELEASE_ID}'
                        rsync -az --delete \
                            .next package.json next.config.ts public node_modules \
                            ${DEPLOY_USER}@211.117.60.189:/home/webservice/react-stage/releases/${RELEASE_ID}/
                        scp .env.production ${DEPLOY_USER}@211.117.60.189:/home/webservice/react-stage/releases/${RELEASE_ID}/.env.production
                        ssh ${DEPLOY_USER}@211.117.60.189 '
                            ln -sfn /home/webservice/react-stage/releases/${RELEASE_ID} /home/webservice/react-stage/current &&
                            cd /home/webservice && pm2 reload sam-front-stage 2>/dev/null || pm2 start react-stage/current/node_modules/.bin/next --name sam-front-stage -- start -p 3100 &&
                            cd /home/webservice/react-stage/releases && ls -1dt */ | tail -n +4 | xargs rm -rf 2>/dev/null || true
                        '
                    """
                }
            }
        }

        // ── 운영 배포 승인 (런칭 후 활성화) ──
        // stage('Production Approval') {
        //     when { branch 'main' }
        //     steps {
        //         slackSend channel: '#product_deploy', color: '#FF9800', tokenCredentialId: 'slack-token',
        //             message: "🔔 *react* 운영 배포 승인 대기 중\n${env.GIT_COMMIT_MSG}\nStage: https://stage.sam.it.kr\n<${env.BUILD_URL}input|승인하러 가기>"
        //         timeout(time: 24, unit: 'HOURS') {
        //             input message: 'Stage 확인 후 운영 배포를 진행하시겠습니까?\nStage: https://stage.sam.it.kr',
        //                   ok: '운영 배포 진행'
        //         }
        //     }
        // }

        // ── main → Production 재빌드 (운영 환경변수) ──
        stage('Rebuild for Production') {
            when { branch 'main' }
            steps {
                sh "cp /var/lib/jenkins/env-files/react/.env.main .env.production"
                sh 'npm run build'
            }
        }

        // ── main → 운영서버 Production 배포 ──
        stage('Deploy Production') {
            when { branch 'main' }
            steps {
                sshagent(credentials: ['deploy-ssh-key']) {
                    sh """
                        ssh ${DEPLOY_USER}@211.117.60.189 'mkdir -p /home/webservice/react/releases/${RELEASE_ID}'
                        rsync -az --delete \
                            .next package.json next.config.ts public node_modules \
                            ${DEPLOY_USER}@211.117.60.189:/home/webservice/react/releases/${RELEASE_ID}/
                        scp .env.production ${DEPLOY_USER}@211.117.60.189:/home/webservice/react/releases/${RELEASE_ID}/.env.production
                        ssh ${DEPLOY_USER}@211.117.60.189 '
                            ln -sfn /home/webservice/react/releases/${RELEASE_ID} /home/webservice/react/current &&
                            cd /home/webservice && pm2 reload sam-front &&
                            cd /home/webservice/react/releases && ls -1dt */ | tail -n +6 | xargs rm -rf 2>/dev/null || true
                        '
                    """
                }
            }
        }
    }

    post {
        success {
            slackSend channel: '#product_infra', color: 'good', tokenCredentialId: 'slack-token',
                message: "✅ *react* 배포 성공 (`${env.BRANCH_NAME}`)\n${env.GIT_COMMIT_MSG}\n<${env.BUILD_URL}|빌드 #${env.BUILD_NUMBER}>"
        }
        failure {
            slackSend channel: '#product_infra', color: 'danger', tokenCredentialId: 'slack-token',
                message: "❌ *react* 배포 실패 (`${env.BRANCH_NAME}`)\n${env.GIT_COMMIT_MSG}\n<${env.BUILD_URL}|빌드 #${env.BUILD_NUMBER}>"
        }
    }
}