name: 🚀 Deploy All Domains to Cloudflare

on:
  push:
    branches: [master, main]
    paths:
      - 'domains/**'
      - '.github/workflows/deploy-cloudflare-all.yml'
  workflow_dispatch:

env:
  CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
  CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}

jobs:
  detect-changes:
    name: 🔍 Detect Changed Domains
    runs-on: ubuntu-latest
    outputs:
      domains: ${{ steps.changes.outputs.domains }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Detect changed domains
        id: changes
        run: |
          CHANGED=$(git diff --name-only HEAD^ HEAD | grep '^domains/' | cut -d'/' -f2 | sort -u | jq -R -s -c 'split("\n")[:-1]')
          echo "domains=$CHANGED" >> $GITHUB_OUTPUT
          echo "Changed domains: $CHANGED"

  deploy-math:
    name: 🧮 Deploy math.blackroad.io
    runs-on: ubuntu-latest
    needs: detect-changes
    if: contains(needs.detect-changes.outputs.domains, 'math-blackroad-io')
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: domains/math-blackroad-io/package-lock.json

      - name: Install dependencies
        working-directory: domains/math-blackroad-io
        run: npm ci

      - name: Build Next.js
        working-directory: domains/math-blackroad-io
        run: npm run build

      - name: Deploy to Cloudflare Pages
        working-directory: domains/math-blackroad-io
        run: npx wrangler pages deploy out --project-name=blackroad-math
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

  deploy-blackroadai:
    name: 🤖 Deploy blackroadai.com
    runs-on: ubuntu-latest
    needs: detect-changes
    if: contains(needs.detect-changes.outputs.domains, 'blackroadai')
    steps:
      - uses: actions/checkout@v4

      - name: Deploy to Cloudflare Pages
        working-directory: domains/blackroadai
        run: npx wrangler pages deploy . --project-name=blackroad-blackroadai
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

  deploy-lucidia-earth:
    name: 🌍 Deploy lucidia.earth
    runs-on: ubuntu-latest
    needs: detect-changes
    if: contains(needs.detect-changes.outputs.domains, 'lucidia-earth')
    steps:
      - uses: actions/checkout@v4

      - name: Deploy to Cloudflare Pages
        working-directory: domains/lucidia-earth
        run: npx wrangler pages deploy . --project-name=blackroad-lucidia-earth
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

  deploy-blackroad-me:
    name: 💜 Deploy blackroad.me
    runs-on: ubuntu-latest
    needs: detect-changes
    if: contains(needs.detect-changes.outputs.domains, 'blackroad-me')
    steps:
      - uses: actions/checkout@v4

      - name: Deploy to Cloudflare Pages
        working-directory: domains/blackroad-me
        run: npx wrangler pages deploy . --project-name=blackroad-blackroad-me
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

  deploy-all-static:
    name: 📦 Deploy All Static Sites
    runs-on: ubuntu-latest
    needs: detect-changes
    strategy:
      matrix:
        domain:
          - blackroad-systems
          - blackroad-network
          - blackroad-quantum
          - lucidia-studio
          - lucidiaqi
          - aliceqi
          - blackroad-inc
    steps:
      - uses: actions/checkout@v4

      - name: Deploy ${{ matrix.domain }}
        working-directory: domains/${{ matrix.domain }}
        run: |
          if [ -f "package.json" ]; then
            npm ci && npm run build
            npx wrangler pages deploy dist --project-name=blackroad-${{ matrix.domain }}
          else
            npx wrangler pages deploy . --project-name=blackroad-${{ matrix.domain }}
          fi
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}

  notify-success:
    name: ✅ Notify Deployment Success
    runs-on: ubuntu-latest
    needs: [deploy-math, deploy-blackroadai, deploy-lucidia-earth, deploy-blackroad-me, deploy-all-static]
    if: always()
    steps:
      - name: Summary
        run: |
          echo "🎉 Automatic Company deployed everything!"
          echo "✅ All domains live on Cloudflare"
          echo "🌐 Changes propagated globally"
