pipeline {
    agent any
    environment {
        PUBLISH_DIR = "${WORKSPACE}/publish"
        DEPLOY_DIR  = '/var/www/server'
    }
    stages {
        stage('Restore') {
            steps {
                sh 'dotnet restore'
            }
        }
        stage('Build') {
            steps {
                sh 'dotnet build --configuration Release --no-restore'
            }
        }
        stage('Publish') {
            steps {
                sh "dotnet publish --configuration Release --output ${PUBLISH_DIR} --no-build"
            }
        }
        stage('Deploy') {
            steps {
                sh """
                    rm -rf ${DEPLOY_DIR}/*
                    cp -r ${PUBLISH_DIR}/* ${DEPLOY_DIR}/
                    sudo systemctl restart print-server
                """
            }
        }
    }
    post {
        always { cleanWs() }
        success { echo 'API deployed successfully!' }
        failure { echo 'API deployment failed!' }
    }
}