pipeline {
    agent any

    environment {
        DOCKER_IMAGE = 'homework-backend'
        DOCKER_TAG = "${env.BUILD_NUMBER}"
        DOCKER_REGISTRY = '192.168.50.221:45000'  // Replace with your Docker registry
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Restore Dependencies') {
            steps {
                sh 'dotnet restore'
            }
        }

        stage('Build') {
            steps {
                sh 'dotnet build --configuration Release --no-restore'
            }
        }

        stage('Test') {
            steps {
                sh 'dotnet test --configuration Release --no-build --verbosity normal'
            }
        }

        stage('Publish') {
            steps {
                sh 'dotnet publish Homework/Homework.csproj --configuration Release --output ./publish --no-build'
            }
        }

        stage('Build Docker Image') {
            steps {
                script {
                    docker.build("${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${DOCKER_TAG}")
                }
            }
        }

        stage('Push Docker Image') {
            steps {
                script {
                    docker.withRegistry("https://${DOCKER_REGISTRY}", 'docker-registry-credentials') {
                        docker.image("${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${DOCKER_TAG}").push()
                        docker.image("${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${DOCKER_TAG}").push('latest')
                    }
                }
            }
        }

        stage('Deploy to Staging') {
            steps {
                script {
                    // Deploy to staging environment
                    sh """
                        docker-compose -f docker-compose.staging.yml down || true
                        sed -i 's|image:.*|image: ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${DOCKER_TAG}|g' docker-compose.staging.yml
                        docker-compose -f docker-compose.staging.yml up -d
                    """
                }
            }
        }

        stage('Integration Tests') {
            steps {
                script {
                    // Run integration tests against staging
                    sh 'echo "Running integration tests..."'
                    // Add your integration test commands here
                }
            }
        }

        stage('Deploy to Production') {
            steps {
                script {
                    // Manual approval for production deployment
                    input message: 'Deploy to Production?', ok: 'Deploy'

                    sh """
                        docker-compose -f docker-compose.prod.yml down || true
                        sed -i 's|image:.*|image: ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${DOCKER_TAG}|g' docker-compose.prod.yml
                        docker-compose -f docker-compose.prod.yml up -d
                    """
                }
            }
        }
    }

    post {
        always {
            // Clean up workspace
            cleanWs()

            // Send notifications
            script {
                def buildStatus = currentBuild.currentResult
                def subject = "Jenkins Build ${buildStatus}: ${env.JOB_NAME} #${env.BUILD_NUMBER}"
                def body = """
                    Build Status: ${buildStatus}
                    Job: ${env.JOB_NAME}
                    Build Number: ${env.BUILD_NUMBER}
                    Build URL: ${env.BUILD_URL}
                """

                emailext(
                    subject: subject,
                    body: body,
                    to: 'team@example.com',
                    attachLog: true
                )
            }
        }

        success {
            echo 'Pipeline completed successfully!'
        }

        failure {
            echo 'Pipeline failed!'
        }
    }
}