name: 🚀 Deploy cloud-mail to Cloudflare Workers

on:
  push:
    branches: [ main ]
    paths:
      - "mail-worker/**"
      - "mail-vue/**"
  workflow_dispatch:

jobs:
  Deploy-cloud-mail:
    name: 🏗️ Build and Deploy
    runs-on: ubuntu-latest

    env:
      CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
      CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
      D1_DATABASE_ID: ${{ secrets.D1_DATABASE_ID }}
      KV_NAMESPACE_ID: ${{ secrets.KV_NAMESPACE_ID }}
      R2_BUCKET_NAME: ${{ secrets.R2_BUCKET_NAME }}
      DOMAIN: ${{ secrets.DOMAIN }}
      ADMIN: ${{ secrets.ADMIN }}
      JWT_SECRET: ${{ secrets.JWT_SECRET }}
      LINUXDO_CLIENT_ID: ${{ secrets.LINUXDO_CLIENT_ID }}
      LINUXDO_CLIENT_SECRET: ${{ secrets.LINUXDO_CLIENT_SECRET }}
      LINUXDO_CALLBACK_URL: ${{ secrets.LINUXDO_CALLBACK_URL }}
      LINUXDO_SWITCH: ${{ secrets.LINUXDO_SWITCH }}

    outputs:
      deployment_skipped: ${{ steps.deploy.outputs.deployment_skipped }}
      worker_url: ${{ steps.deploy.outputs.worker_url }}

    steps:
      - name: ➡️ 检出代码仓库 - Checkout repository
        uses: actions/checkout@v4

      - name: 📦 设置 pnpm - Setup pnpm
        uses: pnpm/action-setup@v4.1.0
        with:
          version: latest

      - name: 📦 设置 Node.js - Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "pnpm"
          cache-dependency-path: "./mail-worker/pnpm-lock.yaml"

      - name: 📥 安装依赖 - Install dependencies
        run: pnpm install --frozen-lockfile
        working-directory: ./mail-worker

      - name: 📡 禁用 Wrangler 遥测 - Disable wrangler telemetry
        working-directory: ./mail-worker
        run: npx wrangler telemetry disable

      - name: 🛠️ 设置环境变量和部署 - Set Secrets and Deploy
        id: deploy
        working-directory: ./mail-worker
        run: |
          if [ -z "$D1_DATABASE_ID" ] || [ -z "$KV_NAMESPACE_ID" ]; then
            echo "⚠️ Required secrets (D1_DATABASE_ID or KV_NAMESPACE_ID) are not set."
            echo "❌ Skipping deployment."
            echo "deployment_skipped=true" >> $GITHUB_OUTPUT
            exit 0
          fi
          
          echo "deployment_skipped=false" >> $GITHUB_OUTPUT
          
          CONFIG_FILE="wrangler-action.toml"
          
          if [ -z "$R2_BUCKET_NAME" ]; then
            sed -i "18,20d" "$CONFIG_FILE"
          fi
          
          echo "⚙️ Dynamically updating '$CONFIG_FILE' with binding IDs..."

          sed -i "s|\${D1_DATABASE_ID}|${D1_DATABASE_ID}|g" "$CONFIG_FILE"
          sed -i "s|\${KV_NAMESPACE_ID}|${KV_NAMESPACE_ID}|g" "$CONFIG_FILE"
          sed -i "s|\${R2_BUCKET_NAME}|${R2_BUCKET_NAME}|g" "$CONFIG_FILE"
          sed -i "s|\"\${DOMAIN}\"|${DOMAIN}|g" "$CONFIG_FILE"
          sed -i "s|\${ADMIN}|${ADMIN}|g" "$CONFIG_FILE"
          sed -i "s|\${JWT_SECRET}|${JWT_SECRET}|g" "$CONFIG_FILE"
          sed -i "s|\${LINUXDO_CLIENT_ID}|${LINUXDO_CLIENT_ID}|g" "$CONFIG_FILE"
          sed -i "s|\${LINUXDO_CLIENT_SECRET}|${LINUXDO_CLIENT_SECRET}|g" "$CONFIG_FILE"
          sed -i "s|\${LINUXDO_CALLBACK_URL}|${LINUXDO_CALLBACK_URL}|g" "$CONFIG_FILE"
          sed -i "s|\${LINUXDO_SWITCH}|${LINUXDO_SWITCH}|g" "$CONFIG_FILE"

          echo "🔍 Debug: Checking configuration after replacement..."
          echo "R2_BUCKET_NAME value: '$R2_BUCKET_NAME'"
          echo "R2 bucket configuration in $CONFIG_FILE:"
          grep -A 2 -B 2 "bucket_name" "$CONFIG_FILE" || true

          echo "🔒 Setting Worker secrets..."
          echo "🚀 Configuration updated. Starting deployment..."
          npx wrangler deploy -c "$CONFIG_FILE" | tee deploy.log | grep -v "https://.*\.workers\.dev"
          WORKER_URL=$(grep -o "https://.*\.workers\.dev" deploy.log)
          echo "::add-mask::$WORKER_URL"
          echo "worker_url=$WORKER_URL" >> $GITHUB_OUTPUT
          echo "✅ Deployment command executed."

      - name: 🗄️ 初始化数据库 - Initialize Database
        if: ${{ steps.deploy.outputs.deployment_skipped == 'false' }}
        run: |
          
          echo "⏳ Waiting 15s before checking initialization status..."
          sleep 15
          
          HTTP_CODE=$(curl -s -w "%{http_code}" -o response.txt "${{ steps.deploy.outputs.worker_url }}/api/init/${JWT_SECRET}")
          RESPONSE_BODY=$(cat response.txt)
          
          echo "🔎 Checking response... (Status: $HTTP_CODE)"

          if [ "$HTTP_CODE" = "200" ] && [ "$RESPONSE_BODY" = "初始化成功" -o "$RESPONSE_BODY" = "Successfully initialized" ]; then
            echo "✅ Database initialization successful!"
          elif [ "$HTTP_CODE" = "200" ]; then
            echo "❌ Database initialization error: $RESPONSE_BODY"
          else
            echo "❌ Database initialization check failed with HTTP status: $HTTP_CODE. response: $RESPONSE_BODY"
          fi

      - name: 📣 部署状态 - Notify Final Status
        if: always()
        run: |
          if [ "${{ job.status }}" == "success" ]; then
            if [ "${{ steps.deploy.outputs.deployment_skipped }}" == "true" ]; then
              echo "❌ Deployment was skipped due to missing configuration."
            else
              echo "🎉🎉🎉 Hooray! Deployment completed successfully! 🎉🎉🎉"
            fi
          else
            echo "❌❌❌ Oh no! The deployment failed. Please check the logs above for errors. ❌❌❌"
          fi

      - name: Delete workflow runs
        uses: GitRML/delete-workflow-runs@main
        with:
          retain_days: '3'
          keep_minimum_runs: '0'
