pipeline {
    agent any

    parameters {
        string(
            name: 'PLATFORM',
            defaultValue: 'vpk120-9082',
            description: 'Target platform for the build'
        )
        string(
            name: 'BUILD_NAME',
            defaultValue: 'build_vpk120_9082',
            description: 'Build directory name'
        )
        booleanParam(
            name: 'SKIP_DOCKER_BUILD',
            defaultValue: false,
            description: 'Skip building the Docker image'
        )
    }

    options {
        buildDiscarder(logRotator(numToKeepStr: '10'))
        timeout(time: 4, unit: 'HOURS')
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
                withCredentials([sshUserPrivateKey(credentialsId: 'github', keyFileVariable: 'SSH_KEY')]) {
                    sh '''
                        eval $(ssh-agent -s)
                        ssh-add $SSH_KEY
                        git submodule update --init --recursive
                        ssh-agent -k
                    '''
                }
            }
        }
        stage('Build') {
            steps {
                script {
                    def buildCmd = "python3 build.py --platform ${params.PLATFORM}"

                    if (params.BUILD_NAME) {
                        buildCmd += " --build-name ${params.BUILD_NAME}"
                    }

                    if (params.SKIP_DOCKER_BUILD) {
                        buildCmd += " --skip-docker-build"
                    }

                    echo "Executing build command: ${buildCmd}"
                    sh buildCmd
                }
            }
        }

        stage('Archive Artifacts') {
            steps {
                script {
                    // Archive build outputs if they exist
                    sh """
                        if [ -d "${params.BUILD_NAME}/tmp/deploy/images" ]; then
                            echo "Build artifacts found in ${params.BUILD_NAME}/tmp/deploy/images"
                        fi
                    """

                    // Archive important build artifacts
                    archiveArtifacts artifacts: "${params.BUILD_NAME}/tmp/deploy/images/**/*.wic.gz,${params.BUILD_NAME}/tmp/deploy/images/**/*.manifest",
                                     allowEmptyArchive: true,
                                     fingerprint: true
                }
            }
        }
    }

    post {
        success {
            echo 'Build completed successfully!'
            slackSend(
                color: 'good',
                message: "Build SUCCESS: ${env.JOB_NAME} #${env.BUILD_NUMBER}\nPlatform: ${params.PLATFORM}\n(<${env.BUILD_URL}|Open>)"
            )
        }
        failure {
            echo 'Build failed!'
            slackSend(
                color: 'danger',
                message: "Build FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER}\nPlatform: ${params.PLATFORM}\n(<${env.BUILD_URL}|Open>)"
            )
        }
    }
}
