name: Deploy CCS CloudFlare Worker

on:
  push:
    branches: [main]
    paths:
      - 'scripts/worker.js'
      - 'scripts/wrangler.toml'
      - 'installers/**'

  # Only run on PRs that touch installer-related files
  pull_request:
    branches: [main]
    paths:
      - 'scripts/worker.js'
      - 'scripts/wrangler.toml'
      - 'installers/**'

  # Allow manual trigger
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    name: Deploy ccs-installer to CloudFlare

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

      - name: Validate Worker (PR Mode)
        if: github.event_name == 'pull_request'
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          workingDirectory: scripts
          wranglerVersion: "4.45.3"
          command: deploy --dry-run

      - name: Deploy Worker (Production)
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          workingDirectory: scripts
          wranglerVersion: "4.45.3"
          command: deploy
