# name: Deploy CloudFunction (pack & optional upload)

# on:
#   push:
#     branches: [ main ]
#   workflow_dispatch: {}

# jobs:
#   deploy:
#     runs-on: ubuntu-latest
#     steps:
#       - name: Checkout
#         uses: actions/checkout@v4

#       - name: Setup Node
#         uses: actions/setup-node@v4
#         with:
#           node-version: '18'

#       - name: Install minimal deps for pack script
#         run: |
#           cd server
#           npm install archiver node-fetch form-data

#       - name: Pack & Upload CloudFunction
#         env:
#           CLOUD_UPLOAD_URL: ${{ secrets.CLOUD_UPLOAD_URL }}
#           CLOUD_UPLOAD_TOKEN: ${{ secrets.CLOUD_UPLOAD_TOKEN }}
#           UNI_PUSH_APPID: ${{ secrets.UNI_PUSH_APPID }}
#         run: |
#           node server/tools/deploy-cloudfunc.js --upload

#       - name: Trigger Deploy API (optional)
#         if: ${{ secrets.CLOUD_DEPLOY_API != '' }}
#         env:
#           CLOUD_DEPLOY_API: ${{ secrets.CLOUD_DEPLOY_API }}
#           CLOUD_DEPLOY_TOKEN: ${{ secrets.CLOUD_DEPLOY_TOKEN }}
#           UPLOAD_URL: ${{ secrets.CLOUD_UPLOAD_URL }}
#         run: |
#           echo "Triggering deploy API: $CLOUD_DEPLOY_API"
#           # POST deploy trigger with basic info; adapt fields to your provider API
#           curl -sS -X POST "$CLOUD_DEPLOY_API" \
#             -H "Authorization: Bearer $CLOUD_DEPLOY_TOKEN" \
#             -H 'Content-Type: application/json' \
#             -d "{ \"uploadUrl\": \"$UPLOAD_URL\" }" \
#             || echo "deploy trigger returned non-zero status"

#       - name: Invoke Cloud Function (test - optional)
#         if: ${{ secrets.CLOUD_FUNC_URL != '' }}
#         env:
#           CLOUD_FUNC_URL: ${{ secrets.CLOUD_FUNC_URL }}
#           PUSH_TOKEN: ${{ secrets.PUSH_TOKEN }}
#           TEST_DEVICE_CID: ${{ secrets.TEST_DEVICE_CID }}
#         run: |
#           echo "Invoking cloud function for smoke test"
#           curl -sS -X POST "$CLOUD_FUNC_URL" \
#             -H 'Content-Type: application/json' \
#             -d "{ \"token\": \"$PUSH_TOKEN\", \"push_clientid\": \"$TEST_DEVICE_CID\", \"title\": \"CI Test\", \"content\": \"hello from CI\", \"payload\": {} }" \
#             || echo "invoke returned non-zero"
