pipeline {
  agent any

  parameters {
    choice(
      name: 'SERVICE',
      choices: ['all', 'tenant-service', 'im-service', 'push-service', 'update-service', 'file-service', 'license-service', 'demo-service'],
      description: '要构建的服务模块（all = 全部，包含所有微服务）'
    )
  }

  environment {
    ACR_REGISTRY   = 'crpi-n44qjpuucgjt8e8c.cn-beijing.personal.cr.aliyuncs.com'
    ACR_NAMESPACE  = 'xuqmgroup'
    ACR_USERNAME   = 'xuqinmin12'
    PROD_HOST      = '106.54.23.149'
    PROD_USER      = 'ubuntu'
    COMPOSE_FILE   = '/opt/xuqm/deploy/compose.production.yaml'
    VERSIONS_FILE  = '/opt/xuqm/deploy/versions.json'
    DOCKER_BUILDKIT = '1'
    VERSION_FILE   = 'VERSION'
  }

  options {
    timeout(time: 60, unit: 'MINUTES')
    buildDiscarder(logRotator(numToKeepStr: '30'))
    disableConcurrentBuilds()
  }

  stages {
    stage('Checkout') {
      steps {
        checkout([
          $class: 'GitSCM',
          branches: [[name: 'main']],
          extensions: [[$class: 'CleanBeforeCheckout']],
          userRemoteConfigs: scm.userRemoteConfigs
        ])
      }
    }

    stage('Resolve Version') {
      steps {
        script {
          def current = fileExists(env.VERSION_FILE) ? readFile(env.VERSION_FILE).trim() : '1.0.0'
          def parts = current.tokenize('.')
          while (parts.size() < 3) parts.add('0')
          def newVer = "${parts[0]}.${parts[1]}.${parts[2].toInteger() + 1}"
          env.SERVICE_VERSION = newVer
          writeFile file: env.VERSION_FILE, text: newVer
          echo "Server: ${current} → ${newVer}"
        }
      }
    }

    stage('Docker Build & Push') {
      steps {
        withCredentials([string(credentialsId: 'ACR_PASSWORD', variable: 'ACR_PASS')]) {
          script {
            def services = params.SERVICE == 'all'
              ? ['tenant-service', 'im-service', 'push-service', 'update-service', 'file-service', 'license-service', 'demo-service']
              : [params.SERVICE]

            for (svc in services) {
              def base = "${ACR_REGISTRY}/${ACR_NAMESPACE}/${svc}"
              def versionedImage = "${base}:${env.SERVICE_VERSION}"
              def latestImage = "${base}:latest"
              echo "Building ${svc} ${env.SERVICE_VERSION}..."
              bat """
                docker login ${ACR_REGISTRY} -u ${ACR_USERNAME} -p %ACR_PASS%
                if %errorlevel% neq 0 exit /b 1
                docker pull --platform=linux/amd64 ${latestImage} || echo Pull failed, will build fresh
                docker build --platform=linux/amd64 --build-arg SERVICE_MODULE=${svc} --build-arg SERVICE_VERSION=${env.SERVICE_VERSION} --build-arg BUILDKIT_INLINE_CACHE=1 --cache-from ${latestImage} -t ${versionedImage} -t ${latestImage} .
                if %errorlevel% neq 0 exit /b 1
                docker push ${versionedImage}
                if %errorlevel% neq 0 exit /b 1
                docker push ${latestImage}
                if %errorlevel% neq 0 exit /b 1
                docker rmi ${versionedImage} ${latestImage}
                exit /b 0
              """
            }
          }
        }
      }
    }

    stage('Deploy to Production') {
      steps {
        lock('prod-deploy') {
          withCredentials([sshUserPrivateKey(credentialsId: 'PROD_SSH_KEY', keyFileVariable: 'SSH_KEY')]) {
            script {
              def services = params.SERVICE == 'all'
                ? ['tenant-service', 'im-service', 'push-service', 'update-service', 'file-service', 'license-service', 'demo-service']
                : [params.SERVICE]

              for (svc in services) {
                def latestImage = "${ACR_REGISTRY}/${ACR_NAMESPACE}/${svc}:latest"
                echo "Deploying ${svc}..."
                retry(2) {
                  bat """
                    ssh -i "%SSH_KEY%" -o StrictHostKeyChecking=no ${PROD_USER}@${PROD_HOST} "docker image prune -f 2>/dev/null || true; docker pull ${latestImage} || exit 1; docker compose -f ${COMPOSE_FILE} up -d --no-deps --force-recreate ${svc} || exit 1; docker image prune -f"
                  """
                }
              }

              // Update versions.json on server for deployed services
              def deployedServices = params.SERVICE == 'all'
                ? ['tenant-service', 'im-service', 'push-service', 'update-service', 'file-service', 'license-service', 'demo-service']
                : [params.SERVICE]
              def releasedAt = new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC'))
              def serviceEntries = deployedServices.collect { svc ->
                "d.setdefault('services', {})['${svc}'] = {'version': '${env.SERVICE_VERSION}', 'changed': True}"
              }.join('\n')
              def updateScript = """\
import json, os
path = '${env.VERSIONS_FILE}'
d = json.load(open(path)) if os.path.exists(path) else {}
${serviceEntries}
d['serviceVersion'] = '${env.SERVICE_VERSION}'
d['releasedAt'] = '${releasedAt}'
json.dump(d, open(path, 'w'), indent=2, ensure_ascii=False)
print('versions.json updated: ${params.SERVICE} = ${env.SERVICE_VERSION}')
""".stripIndent()
              writeFile file: 'update_versions.py', text: updateScript
              bat """
                scp -i "%SSH_KEY%" -o StrictHostKeyChecking=no update_versions.py ${PROD_USER}@${PROD_HOST}:/tmp/update_versions.py
                ssh -i "%SSH_KEY%" -o StrictHostKeyChecking=no ${PROD_USER}@${PROD_HOST} "python3 /tmp/update_versions.py && rm /tmp/update_versions.py"
              """
            }
          }
        }
      }
    }

    stage('Commit Version') {
      steps {
        script {
          bat """
            git config user.email "jenkins@xuqm.com"
            git config user.name "Jenkins CI"
            git add ${env.VERSION_FILE}
            git diff --cached --quiet || git commit -m "ci: bump server to ${env.SERVICE_VERSION} [skip ci]"
            git push origin HEAD:main
          """
        }
      }
    }
  }

  post {
    success { echo "✅ ${params.SERVICE} v${env.SERVICE_VERSION} 构建部署成功" }
    failure  { echo "❌ 构建失败，请检查日志" }
  }
}
