name: Deploy Worker

on:
  push:
    branches:
      - main # Production deployment
      - 'automerge/**' # Dev deployment for automerge branches (matches automerge/*, automerge/**/*, etc.)
  workflow_dispatch: # Allows manual triggering from GitHub UI
    inputs:
      environment:
        description: 'Environment to deploy to'
        required: true
        default: 'dev'
        type: choice
        options:
          - dev
          - production

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

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"

      - name: Install Dependencies
        run: npm ci

      - name: Determine Environment
        id: env
        run: |
          if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
            echo "environment=${{ github.event.inputs.environment }}" >> $GITHUB_OUTPUT
          elif [ "${{ github.ref }}" == "refs/heads/main" ]; then
            echo "environment=production" >> $GITHUB_OUTPUT
          else
            echo "environment=dev" >> $GITHUB_OUTPUT
          fi

      - name: Deploy to Cloudflare Workers (Production)
        if: steps.env.outputs.environment == 'production'
        run: |
          npm install -g wrangler@latest
          # Uses default wrangler.toml (production config) from root directory
          wrangler deploy
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

      - name: Deploy to Cloudflare Workers (Dev)
        if: steps.env.outputs.environment == 'dev'
        run: |
          npm install -g wrangler@latest
          # Uses wrangler.dev.toml for dev environment
          wrangler deploy --config wrangler.dev.toml
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
