pipeline {
    agent { label 'windows-agent' }

    environment {
        REPO_CLONE_URL = 'https://git.compcontrol.de/CompControl/CompControl.git'
        PROJECT_DIR = '.'
        NUGET_SOURCE = 'https://nexus.compcontrol.de/repository/nuget-hosted/'
    }
    
    triggers {
        GenericTrigger(
            causeString: 'Triggered by Gitea Webhook',
            token: 'compcontrol',
            genericVariables: [
                [key: 'ref', value: '$.ref']
            ],
            regexpFilterText: '$ref',
            regexpFilterExpression: 'refs/heads/main',
            printContributedVariables: true,
            printPostContent: false
        )
    }

    stages {
    
        stage('Checkout') {
            steps {
                git branch: 'main', 
                    url: "${env.REPO_CLONE_URL}",
                    credentialsId: '708b5429-833f-4685-8004-c0f916454cbb'
            }
        }
        
        stage('Build') {
            steps {
                dir("${env.PROJECT_DIR}") {
                    bat 'dotnet clean'
                    bat 'if exist bin\\Release rmdir /s /q bin\\Release'
                    bat "\"${tool 'MSBuild_2022'}\\MSBuild.exe\" CompControl.csproj /p:Configuration=Release"
                }
            }
        }
        
        stage('Pack') {
            steps {
                dir("${env.PROJECT_DIR}") {
                    bat 'nuget pack . -Properties Configuration=Release -OutputDirectory bin\\Release'
                }
            }
        }
        
        stage('Deploy') {
            steps {
                dir("${env.PROJECT_DIR}") {
                    script {
                        def nupkgFiles = bat(script: 'dir /b bin\\Release\\*.nupkg 2>nul', returnStatus: true)
                        if (nupkgFiles == 0) {
                            withCredentials([string(credentialsId: 'nuget-api-key', variable: 'NUGET_API_KEY')]) {
                                def pushResult = bat(
                                    script: "dotnet nuget push bin\\Release\\*.nupkg --source ${env.NUGET_SOURCE} --api-key %NUGET_API_KEY%",
                                    returnStatus: true
                                )
                                
                                if (pushResult == 0) {
                                    echo "NuGet package pushed successfully"
                                } else {
                                    echo "NuGet push failed (likely version already exists) - continuing pipeline"
                                    unstable("Package version already exists")
                                }
                            }
                        } else {
                            error "No .nupkg files found in bin\\Release"
                        }
                    }
                }
            }
        }
        
    }
    
    post {
        always {
            cleanWs()
        }
        failure {
            echo 'Pipeline failed!'
        }
        success {
            echo 'Pipeline completed successfully!'
        }
    }
    
}
