name: Deploy to Cloudflare Pages

on:
  push:
    tags:
      - 'deploy-*'   # 只在推送 deploy-* 标签时触发

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Parse tag to env
        run: |
          # 获取 tag 名称(Gitea Actions 使用 GITHUB_REF_NAME)
          TAG_NAME="${GITHUB_REF_NAME}"
          echo "TAG_NAME=$TAG_NAME"

          # Tag 格式: deploy-{project_name}-{deploymentId_no_dashes}
          # 例如: deploy-b7ea026a-cf09-4e31-9f29-b55d7c652b71-123e4567e89b12d3a456426614174000

          # 去掉 "deploy-" 前缀
          PREFIX="deploy-"
          REST="${TAG_NAME#$PREFIX}"

          # deploymentId(无破折号)固定是最后32个字符
          DEPLOYMENT_ID="${REST: -32}"

          # project_name 是剩余部分(去掉最后的 "-" 和 deploymentId)
          PROJECT_NAME="${REST%-${DEPLOYMENT_ID}}"

          echo "PROJECT_NAME=$PROJECT_NAME"   >> "$GITHUB_ENV"
          echo "DEPLOYMENT_ID=$DEPLOYMENT_ID" >> "$GITHUB_ENV"
          #echo "DOMAIN=${PROJECT_NAME}-preview.turingflowai.com" >> "$GITHUB_ENV"

          # 调试输出
          echo "Parsed PROJECT_NAME: $PROJECT_NAME"
          echo "Parsed DEPLOYMENT_ID: $DEPLOYMENT_ID"

      - name: Check toolchain (debug only, 可选)
        run: |
          node -v || echo "node not found"
          npm -v  || echo "npm not found"
          curl --version || echo "curl not found"

      # 已经在 node:20-bookworm-slim 容器内，无需再 setup-node
      # - name: Setup Node
      #   uses: actions/setup-node@v4
      #   with:
      #     node-version: '20'

      - name: Use CN npm registry
        run: |
          npm config set registry http://repo.myhuaweicloud.com/repository/npm/

      - name: Install dependencies
        run: |
          npm ci --no-audit --no-fund

      - name: Build
        run: npm run build

      - name: Deploy to Cloudflare Pages
        env:
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
          CLOUDFLARE_API_TOKEN:  ${{ secrets.CF_API_TOKEN }}
          PROJECT_NAME:          ${{ env.PROJECT_NAME }}
          DOMAIN:                ${{ env.DOMAIN }}
        run: |
          set -euo pipefail
          echo "[deploy] project: $PROJECT_NAME"
          echo "[deploy] domain:  $DOMAIN"

          # 部署到 Cloudflare Pages (假定构建产物在 dist/)
          npx wrangler pages deploy dist \
            --project-name "$PROJECT_NAME" \
            --branch main

          # 绑定自定义域名：<project_name>-preview.turingflowai.com
          #curl --fail -X POST \
          #  "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/pages/projects/${PROJECT_NAME}/domains" \
          #  -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
          #  -H "Content-Type: application/json" \
          #  -d '{"name":"'"${DOMAIN}"'"}' \
          #  || true

      - name: Notify Deploy Service (success)
        if: success()
        env:
          DEPLOY_SERVICE_CALLBACK_URL: ${{ secrets.DEPLOY_SERVICE_CALLBACK_URL }}
          DEPLOY_SERVICE_TOKEN:        ${{ secrets.DEPLOY_SERVICE_TOKEN }}
          DEPLOYMENT_ID:               ${{ env.DEPLOYMENT_ID }}
        run: |
          set -euo pipefail

          # 获取当前 commit SHA (Gitea Actions 使用 GITHUB_SHA)
          COMMIT_SHA="${GITHUB_SHA}"

          curl -X POST "$DEPLOY_SERVICE_CALLBACK_URL" \
            -H "Content-Type: application/json" \
            -H "Authorization: Bearer $DEPLOY_SERVICE_TOKEN" \
            -d '{
              "deploymentId": "'"${DEPLOYMENT_ID}"'",
              "status": "deployed",
              "commitSha": "'"${COMMIT_SHA}"'",
              "cfDeploymentId": "",
              "errorMessage": null
            }'

      - name: Notify Deploy Service (failure)
        if: failure()
        env:
          DEPLOY_SERVICE_CALLBACK_URL: ${{ secrets.DEPLOY_SERVICE_CALLBACK_URL }}
          DEPLOY_SERVICE_TOKEN:        ${{ secrets.DEPLOY_SERVICE_TOKEN }}
          DEPLOYMENT_ID:               ${{ env.DEPLOYMENT_ID }}
        run: |
          set -euo pipefail

          # 获取当前 commit SHA
          COMMIT_SHA="${GITHUB_SHA}"

          curl -X POST "$DEPLOY_SERVICE_CALLBACK_URL" \
            -H "Content-Type: application/json" \
            -H "Authorization: Bearer $DEPLOY_SERVICE_TOKEN" \
            -d '{
              "deploymentId": "'"${DEPLOYMENT_ID}"'",
              "status": "failed",
              "commitSha": "'"${COMMIT_SHA}"'",
              "cfDeploymentId": "",
              "errorMessage": "see Gitea Actions logs"
            }'
