pipeline {
    agent any

    triggers {
        // Every Sunday at 07:15
        cron('15 21 * * 0')
    }

    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 {
                sh """
                    python3 -m venv venv
                    . venv/bin/activate
                    pip install --upgrade pip --quiet
                    pip install --quiet atproto
                """
            }
        }

        stage('Post Dijous') {
            steps {
                sh """
                    . venv/bin/activate
                    python3 bsky_post.py "DEMÀ DILLUNS!!!!!!!!!!!!" \\
                        --username "\$BSKY_HANDLE" \\
                        --password "\$BSKY_APP_PASSWORD" \\
                        --image media/diumenge.mp4 \\
                        --alt "DEMÀ DILLUNS!!!!!!!!!!!!" \\
                        --lang ca
                """
            }
        }
    }

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