name: Deploy Web (production)

# Builds apps/web and publishes it to the PRODUCTION Cloudflare Pages project
# (bloom-dashboard-prod). Manual-only, mirroring the API's deploy-production.yml - production is a
# deliberate release, never automatic.
#
# INERT until a production API exists. Before running this for real:
#   1. Stand up a production API (env=production) and note its origin.
#   2. On the bloom-dashboard-prod Pages project, set the Function env var API_ORIGIN to that
#      production API origin (Pages -> Settings -> Environment variables). Until then the proxy
#      falls back to the dev API.
#   3. Register the prod dashboard's OAuth redirect URI
#      (https://bloom-dashboard-prod.pages.dev/api/auth/google/callback) in the Google client, and
#      set the production API's BLOOM_DASHBOARD_URL / BLOOM_OAUTH_REDIRECT_BASE_URL to that origin.
#
# The SPA builds same-origin (empty VITE_API_BASE_URL); the per-project API_ORIGIN (step 2) is what
# points production web at the production API.

on:
  workflow_dispatch:

concurrency:
  group: deploy-web-production
  cancel-in-progress: false

jobs:
  deploy:
    if: ${{ vars.CLOUDFLARE_ACCOUNT_ID != '' }}
    runs-on: bloom-arc
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v4
        with:
          version: 10.6.3

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: pnpm
          cache-dependency-path: pnpm-lock.yaml

      # Workspace-root install (single lockfile, #481); build + publish stay in apps/web.
      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Build
        working-directory: apps/web
        env:
          VITE_API_BASE_URL: ${{ vars.VITE_API_BASE_URL_PROD }}
        run: pnpm run build

      - name: Publish to Cloudflare Pages (production)
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ vars.CLOUDFLARE_ACCOUNT_ID }}
          workingDirectory: apps/web
          packageManager: pnpm
          command: >-
            pages deploy dist
            --project-name=${{ vars.CLOUDFLARE_PAGES_PROJECT_PROD || 'bloom-dashboard-prod' }}
            --branch=main
