pipeline {
    agent any

    triggers {
        // Every Thursday at 07:15
        cron('15 7 * * 4')
    }

    environment {
        BSKY_HANDLE          = credentials('BSKY_GROMENAWARE_HANDLE')
        BSKY_APP_PASSWORD    = credentials('BSKY_GROMENAWARE_APP_PASSWORD')
    }

    options {
        timeout(time: 10, unit: 'MINUTES')
        buildDiscarder(logRotator(numToKeepStr: '30'))
    }

    stages {
        stage('Install dependencies') {
            steps {
                # Create a virtual environment named 'venv'
                python3 -m venv venv
                
                # Activate the virtual environment and install dependencies
                . venv/bin/activate && \
                pip install --upgrade pip && \
                pip install --quiet atproto
            }
        }

        stage('Post Dijous') {
            steps {
                sh """
                    # Activate the virtual environment and run the script
                    . venv/bin/activate && \
                    python3 bsky_post.py "Feliç dijous!!!!" \\
                        --username "\$BSKY_HANDLE" \\
                        --password "\$BSKY_APP_PASSWORD" \\
                        --image media/dijous.jpg \\
                        --alt "Feliç dijous!!" \\
                        --lang ca
                """
            }
        }
    }

    post {
        success {
            echo '✅ Dijous post published successfully.'
        }
        failure {
            echo '❌ Dijous post failed.'
        }
    }
}