pipeline {
    agent any

    options {
        ansiColor('xterm')
        timestamps()
        buildDiscarder logRotator(
            artifactDaysToKeepStr: '30',
            artifactNumToKeepStr: '100',
            daysToKeepStr: '365',
            numToKeepStr: '1000'
        )
        timeout(time: 6000, unit: 'MINUTES')
    }

  // No parameters needed for CI/CD tests and linting
  // For feed execution with parameters, use sme-feed-file-exporter.Jenkinsfile

  environment {
    PYTHON_VERSION = '3.11.2'
    LOGGER_NAME = 'sme-feed-file-exporter'
  }

  stages {
    stage('Prep') {
      steps {
        echo 'Preparing Python environment'
        sh '''
            python3 --version

        '''
      }
    }
    stage('Setup Python Environment') {
      steps {
          sh '''
              python3 -m venv venv
              . venv/bin/activate
              pip install --upgrade pip
              pip install -r requirements.txt
              pip install -r requirements-dev.txt
          '''
      }
    }
    stage('Lint') {
      steps {
        script {
          try {
            sh '''
              . venv/bin/activate
              flake8 . --config=.flake8 --count --show-source --statistics
            '''
          } catch (Exception e) {
            currentBuild.result = 'UNSTABLE'
            error("Linting failed: ${e.message}")
          }
        }
      }
    }
    stage('Test') {
      steps {
        script {
          try {
            sh '''
              . venv/bin/activate
              pytest tests/ -v --cov=. --cov-report=term-missing --cov-report=xml
            '''
          } catch (Exception e) {
            currentBuild.result = 'FAILURE'
            error("Tests failed: ${e.message}")
          }
        }
      }
    }
  }
    post {
        always {
            script {
                // Clean up virtual environment
                sh 'rm -rf venv'
            }
        }
        success {
          echo 'Feed export completed.'
        }
        failure {
          echo 'Job failed.'
        }
        unstable {
            echo 'Job completed with warnings!'
        }
    }
}

// CI/CD JENKINSFILE: Runs tests and linting only
// For actual feed export execution with all parameters, use sme-feed-file-exporter.Jenkinsfile
