pipeline {
    agent any

    environment {
        IMAGE_XHS = 'social-mcp-xhs'
        IMAGE_XHH = 'social-mcp-xhh'
        APP_XHS = 'mcp-xhs'
        APP_XHH = 'mcp-xhh'
    }

    triggers {
        GenericTrigger(token: 'social-mcp-deploy')
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Build Docker Images') {
            steps {
                sh "docker build --build-arg APP_NAME=xhs-mcp --build-arg HTTP_PROXY=http://172.17.0.1:7897 --build-arg HTTPS_PROXY=http://172.17.0.1:7897 -t ${IMAGE_XHS}:${BUILD_NUMBER} -t ${IMAGE_XHS}:latest ."
                sh "docker build --build-arg APP_NAME=xhh-mcp --build-arg HTTP_PROXY=http://172.17.0.1:7897 --build-arg HTTPS_PROXY=http://172.17.0.1:7897 -t ${IMAGE_XHH}:${BUILD_NUMBER} -t ${IMAGE_XHH}:latest ."
            }
        }

        stage('Deploy') {
            steps {
                sh """
                    docker stop ${APP_XHS} || true
                    docker rm ${APP_XHS} || true
                    docker stop ${APP_XHH} || true
                    docker rm ${APP_XHH} || true

                    mkdir -p /data/${APP_XHS}
                    mkdir -p /data/${APP_XHH}
                    chown 1001:1001 /data/${APP_XHS}
                    chown 1001:1001 /data/${APP_XHH}

                    docker run -d \
                        --name ${APP_XHS} \
                        --network nginx \
                        -p 9527:9527 \
                        --shm-size=1g \
                        -v /data/${APP_XHS}:/home/appuser/.social-mcp-xhs \
                        -e NODE_ENV=production \
                        -e HOST=0.0.0.0 \
                        -e PORT=9527 \
                        -e HEADLESS=true \
                        -e COOKIE_DIR=/home/appuser/.social-mcp-xhs \
                        -e APP_NAME=xhs-mcp \
                        -e ALLOW_REMOTE=yes-i-understand-the-risk \
                        --restart unless-stopped \
                        ${IMAGE_XHS}:latest

                    docker run -d \
                        --name ${APP_XHH} \
                        --network nginx \
                        -p 9528:9528 \
                        --shm-size=1g \
                        -v /data/${APP_XHH}:/home/appuser/.social-mcp-xhh \
                        -e NODE_ENV=production \
                        -e HOST=0.0.0.0 \
                        -e PORT=9528 \
                        -e HEADLESS=true \
                        -e COOKIE_DIR=/home/appuser/.social-mcp-xhh \
                        -e APP_NAME=xhh-mcp \
                        -e ALLOW_REMOTE=yes-i-understand-the-risk \
                        --restart unless-stopped \
                        ${IMAGE_XHH}:latest
                """
            }
        }
    }

    post {
        success {
            echo "Deployed ${APP_XHS} and ${APP_XHH} build #${BUILD_NUMBER} successfully"
        }
        failure {
            echo "Build #${BUILD_NUMBER} failed"
        }
    }
}
