pipeline {
    agent any

    environment {
        REGISTRY = 'atl.vultrcr.com/vllm'
        IMAGE_NAME = 'kimi-k26-dflash-mi300x'
    }

    parameters {
        string(name: 'IMAGE_TAG', defaultValue: 'nightly', description: 'Docker image tag')
        string(name: 'GIT_REPO', defaultValue: 'https://sweetapi.com/biondizzle/kimi-k26-dflash-mi300x.git', description: 'Git repository URL')
        string(name: 'GIT_BRANCH', defaultValue: 'master', description: 'Git branch to build')
    }

    stages {
        stage('Checkout') {
            steps {
                script {
                    if (params.GIT_REPO) {
                        git url: params.GIT_REPO, branch: params.GIT_BRANCH
                    }
                }
            }
        }

        stage('Build') {
            steps {
                script {
                    withCredentials([string(credentialsId: 'HF_TOKEN', variable: 'HF_SECRET')]) {
                        docker.withRegistry("https://${REGISTRY}", 'ATL_VCR_VLLM') {
                            def imageTag = params.IMAGE_TAG
                            sh "docker build -f Dockerfile.kimi26-dflash --build-arg HF_TOKEN=\${HF_SECRET} -t ${REGISTRY}/${IMAGE_NAME}:${imageTag} ."
                        }
                    }
                }
            }
        }

        stage('Push') {
            steps {
                script {
                    docker.withRegistry("https://${REGISTRY}", 'ATL_VCR_VLLM') {
                        docker.image("${REGISTRY}/${IMAGE_NAME}:${params.IMAGE_TAG}").push()
                    }
                }
            }
        }
    }

    post {
        success {
            echo "✅ Image pushed: ${REGISTRY}/${IMAGE_NAME}:${params.IMAGE_TAG}"
        }
        failure {
            echo "❌ Build failed"
        }
    }
}
