pipeline {
    agent any

    parameters {
        string(
            name: 'NODE_IP',
            defaultValue: '192.168.108.200',
            description: 'On-premise Kubernetes node IP'
        )
    }

    environment {
        HARBOR    = '192.168.108.200:80'
        IMAGE     = '192.168.108.200:80/vaishnavi-ecommerce/admin'
        OLD_IMAGE = '192.168.49.2:30004/vaishnavi-ecommerce/admin:latest'
        TAG       = "${env.BUILD_NUMBER}"
        NAMESPACE = 'ecommerce'
        APP_NAME  = 'VC E-Commerce Admin Panel'
    }

    stages {
        stage('Build Image') {
            steps {
                sh """
                    docker build \
                        --build-arg VITE_API_URL=http://${params.NODE_IP}:30080/api \
                        --build-arg "VITE_APP_NAME=${APP_NAME}" \
                        -t ${IMAGE}:${TAG} \
                        -t ${IMAGE}:latest \
                        .
                """
            }
        }

        stage('Push to Harbor') {
            steps {
                withCredentials([usernamePassword(
                    credentialsId: 'harbor-credentials',
                    usernameVariable: 'HARBOR_USER',
                    passwordVariable: 'HARBOR_PASS'
                )]) {
                    sh """
                        echo "\$HARBOR_PASS" | docker login ${HARBOR} -u "\$HARBOR_USER" --password-stdin
                        docker push ${IMAGE}:${TAG}
                        docker push ${IMAGE}:latest
                    """
                }
            }
        }

        stage('Deploy') {
            steps {
                withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
                    sh """
                        sed "s|${OLD_IMAGE}|${IMAGE}:${TAG}|g" \
                            k8s/deployment.yaml | kubectl apply -f -
                        kubectl apply -f k8s/service.yaml
                        kubectl apply -f k8s/ingress.yaml
                    """
                }
            }
        }

        stage('Verify Rollout') {
            steps {
                withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
                    sh "kubectl rollout status deployment/admin -n ${NAMESPACE} --timeout=180s"
                }
            }
        }

        stage('Smoke Test') {
            steps {
                sh """
                    STATUS=\$(curl -o /dev/null -sw "%{http_code}" http://${params.NODE_IP}:30082)
                    echo "HTTP status: \$STATUS"
                    [ "\$STATUS" = "200" ]
                """
            }
        }
    }

    post {
        always {
            sh "docker rmi ${IMAGE}:${TAG} || true"
            sh "docker rmi ${IMAGE}:latest || true"
        }
        success {
            echo "Deploy successful. Admin panel accessible at http://${params.NODE_IP}:30082"
        }
        failure {
            withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
                sh "kubectl get pods -n ${NAMESPACE} || true"
                sh "kubectl describe deployment/admin -n ${NAMESPACE} || true"
            }
        }
    }
}
