pipeline {
    agent any

    environment {
        CLOUDFLARE_API_TOKEN = 'cfut_cYi1Do6CdWPGD8ecrJ6ZtnOMcjU2GI6LdEToDEct71ff4034'
        CF_ACCOUNT_ID = '0a0b9995a00648aadf505e235e66458a'
    }

    stages {
        stage('Build') {
            steps {
                echo 'Building VitePress site...'
                sh 'npm ci && npm run build'
            }
        }

        stage('Deploy') {
            steps {
                echo 'Deploying to Cloudflare Pages via wrangler...'
                sh '''
                rm -f wrangler.toml
                cat > wrangler.toml << EOF
name = "web-home"
account_id = "$CF_ACCOUNT_ID"
pages_project = "web-home"
compatibility_date = "2024-01-01"
EOF
                wrangler pages deploy docs/.vitepress/dist
                '''
            }
        }
    }

    post {
        success {
            echo 'Deployment successful!'
        }
        failure {
            echo 'Deployment failed!'
        }
    }
}