pipeline {
    agent { label 'REPLACE_TRUSTED_AGENT_LABEL' }

    options {
        skipDefaultCheckout()
        disableConcurrentBuilds()
        timeout(time: 60, unit: 'MINUTES')
        buildDiscarder(logRotator(numToKeepStr: '20'))
        timestamps()
    }

    environment {
        APP_DIR = 'secure-delivery'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
                sh '''
                    set -eu
                    git rev-parse HEAD
                    python3 --version
                '''
            }
        }

        stage('Python Syntax Check') {
            steps {
                dir('secure-delivery') {
                    sh '''
                        set -eu
                        python3 -m py_compile app.py test_app.py
                    '''
                }
            }
        }

        stage('Unit Tests') {
            steps {
                dir('secure-delivery') {
                    sh '''
                        set -eu
                        python3 -m venv .venv
                        . .venv/bin/activate
                        python -m pip install -r requirements.txt
                        python -m unittest -v
                    '''
                }
            }
        }

        stage('Security Gates') {
            steps {
                error(
                    'Chapter 6 security gates are not yet configured. ' +
                    'Sergio must integrate verified dependency and secret ' +
                    'scanners before enabling release execution.'
                )
            }
        }

        stage('Build and Publish') {
            when {
                branch 'main'
            }
            steps {
                error(
                    'Release execution is disabled until trusted-agent, ' +
                    'registry, image scanning, and credential controls ' +
                    'have been configured and verified.'
                )
            }
        }

        stage('Deploy Staging') {
            when {
                branch 'main'
            }
            steps {
                error(
                    'Staging deployment requires verified Ansible, SSH, ' +
                    'immutable image digest, and HTTPS configuration.'
                )
            }
        }

        stage('Production Approval') {
            when {
                branch 'main'
            }
            steps {
                error(
                    'Production approval must be implemented with an ' +
                    'authorized submitter restriction and timeout.'
                )
            }
        }

        stage('Deploy Production') {
            when {
                branch 'main'
            }
            steps {
                error(
                    'Production deployment is disabled until all release ' +
                    'gates and authorized approval are operational.'
                )
            }
        }
    }

    post {
        always {
            echo 'Chapter 6 pipeline execution finished.'
        }

        success {
            echo 'All configured stages passed.'
        }

        failure {
            echo 'Pipeline failed or was blocked by an incomplete gate.'
        }
    }
}