pipeline {
    agent any

    environment {
        APP_VERSION = '1.0.0'
        DEPLOY_ENV = 'staging'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
                echo 'Repository cloned successfully'
            }
        }

        stage('Build') {
            steps {
                sh 'mvn clean compile'
                sh 'mvn package -DskipTests'
                echo 'Build completed'
            }
        }

        stage('Test') {
            steps {
                sh 'mvn test'
                dir('reports') {
                    sh 'ls -la'
                }
            }
        }

        stage('Deploy to Staging') {
            when {
                branch 'main'
            }
            steps {
                sh 'kubectl apply -f deployment-staging.yaml'
                echo 'Deployed to staging'
            }
        }

        stage('Deploy to Production') {
            when {
                branch 'main'
            }
            steps {
                withCredentials([string(credentialsId: 'prod-token', variable: 'TOKEN')]) {
                    sh 'deploy-prod.sh'
                }
                echo 'Deployed to production'
            }
        }
    }
}
