pipeline {
    agent any
    environment {
        DOTNET_CLI_TELEMETRY_OPTOUT = '1'
        DEPLOY_API   = '/mnt/h/iis/bitforum-api/sources'
        DEPLOY_ADMIN = '/mnt/h/iis/bitforum-admin/sources'
    }
    stages {
        stage('Build') {
            steps {
                sh 'dotnet publish Web.Api -c Release -r win-x64 --self-contained false -o publish/api'
                sh 'dotnet publish Admin -c Release -r win-x64 --self-contained false -o publish/admin'
            }
        }
        stage('Stop Services') {
            agent { label 'windows-host' }
            steps {
                bat 'sc.exe stop bitforum-Api || exit 0'
                bat 'sc.exe stop bitforum-Admin || exit 0'
                bat 'ping 127.0.0.1 -n 6 > nul'
            }
        }
        stage('Deploy API') {
            steps {
                sh 'rsync -av --delete --exclude="web.config" --exclude="restart.ps1" publish/api/ ${DEPLOY_API}/'
            }
        }
        stage('Deploy Admin') {
            steps {
                sh 'rsync -av --delete --exclude="web.config" --exclude="restart.ps1" publish/admin/ ${DEPLOY_ADMIN}/'
            }
        }
        stage('Start Services') {
            agent { label 'windows-host' }
            steps {
                bat 'sc.exe start bitforum-Api'
                bat 'sc.exe start bitforum-Admin'
            }
        }
    }
    post {
        success { echo 'Backend 배포 성공!' }
        failure { echo 'Backend 배포 실패!' }
    }
}
