pipeline {
    agent any

    environment {
        CLOUDFLARE_API_TOKEN = 'cfut_cYi1Do6CdWPGD8ecrJ6ZtnOMcjU2GI6LdEToDEct71ff4034'
    }

    stages {
        stage('Install') {
            steps {
                echo 'Installing dependencies...'
                sh 'npm ci'
            }
        }

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

        stage('Deploy') {
            steps {
                echo 'Deploying to Cloudflare Pages...'
                sh '''cd docs/.vitepress/dist && tar -czf /tmp/dist.tar.gz * && cd /tmp && curl -s -X POST "https://api.cloudflare.com/client/v4/accounts/0a0b9995a00648aadf505e235e66458a/pages/projects/web-home/deployments" -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -F "file=@dist.tar.gz" -F "manifest={\\"version\\":1,\\"files\\":{\\"\\":\\"build\\"}}" -F "metadata={\\"branch\\":\\"main\\",\\"commit_hash\\":\\"$GIT_COMMIT\\",\\"skip_caching\\":true}"'''
            }
        }
    }

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