name: Deploy CultRoll Admin App

on:
  push:
    branches: [main]
    paths:
      - "src/sites/cultroll-admin/**"
      - "src/sites/cultroll/lib/**"
      - "astro.config.cultroll-admin.mjs"
      - "wrangler.cultroll-admin.toml"
      - "package.json"
      - "package-lock.json"
      - "tsconfig.json"
      - "biome.json"
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      deployments: write
    steps:
      - uses: actions/checkout@v4
        with:
          lfs: true

      - uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: npm

      - run: npm ci
      - run: npm run build:cultroll-admin

      - name: Create Pages project
        continue-on-error: true
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        run: |
          curl -s -X POST \
            "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/pages/projects" \
            -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
            -H "Content-Type: application/json" \
            -d '{"name":"cultroll-admin-web","production_branch":"main"}'

      - name: Prepare wrangler config
        run: cp wrangler.cultroll-admin.toml wrangler.toml

      - name: Set Pages bindings and env vars
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          SCRAPER_ADMIN_TOKEN: ${{ secrets.SCRAPER_ADMIN_TOKEN }}
        run: |
          DB_ID="55ce7f8b-4b90-45f5-a095-69c6266926f1"
          R2_BINDING='{"IMAGES":{"name":"cultroll-images"}}'
          ACCOUNT_SUBDOMAIN=$(curl -s "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/workers/subdomain" \
            -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" | jq -r '.result.subdomain // empty')
          if [ -n "$ACCOUNT_SUBDOMAIN" ]; then
            SCRAPER_URL="https://cultroll-scraper.${ACCOUNT_SUBDOMAIN}.workers.dev"
          else
            SCRAPER_URL="https://cultroll-scraper.workers.dev"
          fi
          if [ -z "${SCRAPER_ADMIN_TOKEN:-}" ]; then
            echo "::error::SCRAPER_ADMIN_TOKEN secret is required"
            exit 1
          fi
          ENV_VARS=$(jq -nc \
            --arg scraperUrl "${SCRAPER_URL}" \
            --arg scraperToken "${SCRAPER_ADMIN_TOKEN}" \
            '{
              SCRAPER_WORKER_URL: { value: $scraperUrl },
              SCRAPER_ADMIN_TOKEN: { value: $scraperToken, type: "secret_text" }
            }')
          curl -s -X PATCH \
            "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/pages/projects/cultroll-admin-web" \
            -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
            -H "Content-Type: application/json" \
            -d "{\"deployment_configs\":{\"production\":{\"d1_databases\":{\"DB\":{\"id\":\"${DB_ID}\"}},\"r2_buckets\":${R2_BINDING},\"env_vars\":${ENV_VARS}},\"preview\":{\"d1_databases\":{\"DB\":{\"id\":\"${DB_ID}\"}},\"r2_buckets\":${R2_BINDING},\"env_vars\":${ENV_VARS}}}}"

      - name: Deploy to Cloudflare Pages
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy dist/cultroll-admin --project-name cultroll-admin-web --branch main

      - name: Add custom domain
        continue-on-error: true
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        run: |
          curl -s -X POST \
            "https://api.cloudflare.com/client/v4/accounts/${CLOUDFLARE_ACCOUNT_ID}/pages/projects/cultroll-admin-web/domains" \
            -H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
            -H "Content-Type: application/json" \
            -d '{"name":"portal.cultroll.com"}'
