pipeline {
    agent any

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

    triggers {
        cron('H/30 * * * *')
    }

    stages {
        stage('Checkout Code') {
            steps {
                // Pulls the code from the repository where this Jenkinsfile lives
                checkout scm
            }
        }

        stage('Setup Python & Install Dependencies') {
            steps {
                sh '''
                    set -e

                    # ── Create venv ────────────────────────────────────
                    python3 -m venv venv

                    # ── Upgrade pip toolchain ──────────────────────────
                    . venv/bin/activate
                    pip install --upgrade pip wheel setuptools

                    # ── Core dependencies ──────────────────────────────
                    pip install -U \
                        atproto \
                        playwright \
                        playwright-stealth \
                        httpx \
                        arrow \
                        python-dotenv \
                        moviepy \
                        beautifulsoup4 \
                        charset-normalizer \
                        Pillow \
                        grapheme

                    # ── yt-dlp: always upgrade to latest ──────────────
                    pip install --upgrade yt-dlp
                    pip show yt-dlp | grep -E "^(Name|Version)"

                    # ── curl_cffi: TikTok impersonation support ────────
                    # Required by yt-dlp to bypass TikTok bot detection
                    pip install --upgrade curl-cffi
                    pip show curl-cffi | grep -E "^(Name|Version)"

                    # ── playwright-stealth version check ───────────────
                    pip show playwright-stealth | grep -E "^(Name|Version)"
                    python3 -c "
                    try:
                        from playwright_stealth import stealth_sync
                        print('playwright_stealth OK (v1.x - stealth_sync)')
                    except ImportError:
                        from playwright_stealth import Stealth
                        print('playwright_stealth OK (v2.x - Stealth class)')
"

                    # ── Sanity checks ──────────────────────────────────
                    python3 -c "import atproto;    print('atproto    OK')"
                    python3 -c "import playwright;  print('playwright OK')"
                    python3 -c "import yt_dlp;      print('yt_dlp     OK')"
                    python3 -c "import curl_cffi;   print('curl_cffi  OK')"
                    python3 -c "import httpx;       print('httpx      OK')"
                    python3 -c "import arrow;       print('arrow      OK')"
                    python3 -c "import moviepy;     print('moviepy    OK')"

                    # ── System tools ───────────────────────────────────
                    ffmpeg  -version | head -1
                    ffprobe -version | head -1

                    # ── Playwright browser binaries ────────────────────
                    playwright install chromium
                '''
            }
        }


        stage('Run Script') {
            steps {
                // Securely injects Jenkins credentials as environment variables
                withCredentials([
                    string(credentialsId: 'TWITTER_USERNAME', variable: 'TWITTER_USERNAME'),
                    string(credentialsId: 'TWITTER_PASSWORD', variable: 'TWITTER_PASSWORD'),
                    string(credentialsId: 'TWITTER_COMEDYGOLDBCN_EMAIL', variable: 'TWITTER_COMEDYGOLDBCN_EMAIL'),
                    string(credentialsId: 'TWITTER_COMEDYGOLDBCN_HANDLE', variable: 'TWITTER_COMEDYGOLDBCN_HANDLE'),
                    string(credentialsId: 'BSKY_COMEDYGOLDBCN_HANDLE', variable: 'BSKY_COMEDYGOLDBCN_HANDLE'),
                    string(credentialsId: 'BSKY_COMEDYGOLDBCN_APP_PASSWORD', variable: 'BSKY_COMEDYGOLDBCN_APP_PASSWORD')
                ]) {
                    sh '''
                    # Activate the virtual environment and run the script
                    . venv/bin/activate && \
                    python3 twitter2bsky.py \
                        --twitter-username "$TWITTER_USERNAME" \
                        --twitter-password "$TWITTER_PASSWORD" \
                        --twitter-email "$TWITTER_COMEDYGOLDBCN_EMAIL" \
                        --twitter-handle "$TWITTER_COMEDYGOLDBCN_HANDLE" \
                        --bsky-handle "$BSKY_COMEDYGOLDBCN_HANDLE" \
                        --bsky-password "$BSKY_COMEDYGOLDBCN_APP_PASSWORD" \
                        --bsky-langs ca
                    '''
                }
            }
        }
    }
}