name: Deploy Everything (Multi-Cloud)

on:
  push:
    branches: [main, master]
  workflow_dispatch:
    inputs:
      platform:
        description: 'Platform to deploy'
        required: false
        default: 'all'
        type: choice
        options:
          - all
          - cloudflare
          - railway
          - droplet

jobs:
  # Phase 1: Deploy Static Sites to Cloudflare Pages
  deploy-cloudflare:
    if: github.event.inputs.platform == 'all' || github.event.inputs.platform == 'cloudflare' || github.event.inputs.platform == ''
    runs-on: ubuntu-latest
    strategy:
      matrix:
        site:
          - blackroad-network
          - blackroad-systems
          - blackroad-me
          - lucidia-earth
          - aliceqi
          - blackroad-inc
          - blackroadai
          - lucidia-studio
          - lucidiaqi
          - blackroad-quantum

    steps:
      - uses: actions/checkout@v4

      - name: Deploy ${{ matrix.site }}
        uses: cloudflare/pages-action@v1
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          projectName: ${{ matrix.site }}
          directory: domains/${{ matrix.site }}
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}

  # Phase 2: Deploy Backend Services to Railway
  deploy-railway:
    if: github.event.inputs.platform == 'all' || github.event.inputs.platform == 'railway' || github.event.inputs.platform == ''
    runs-on: ubuntu-latest
    needs: [deploy-cloudflare]

    steps:
      - uses: actions/checkout@v4

      - name: Install Railway CLI
        run: npm install -g @railway/cli

      - name: Deploy Core Services
        run: |
          echo "🚂 Deploying to Railway..."

          # Link to Railway project
          railway link ${{ secrets.RAILWAY_PROJECT_ID }}

          # Deploy services (Railway will detect changes automatically)
          railway up

        env:
          RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}

  # Phase 3: Deploy Custom Services to Droplet
  deploy-droplet:
    if: github.event.inputs.platform == 'all' || github.event.inputs.platform == 'droplet' || github.event.inputs.platform == ''
    runs-on: ubuntu-latest
    needs: [deploy-cloudflare, deploy-railway]

    steps:
      - uses: actions/checkout@v4

      - name: Deploy to Droplet
        uses: appleboy/ssh-action@v1.0.0
        with:
          host: ${{ secrets.DROPLET_IP }}
          username: root
          key: ${{ secrets.DROPLET_SSH_KEY }}
          script: |
            cd /opt/blackroad
            git pull origin main
            docker-compose up -d --build
            docker-compose ps

  # Phase 4: Update DNS Records
  update-dns:
    runs-on: ubuntu-latest
    needs: [deploy-cloudflare, deploy-railway, deploy-droplet]

    steps:
      - uses: actions/checkout@v4

      - name: Update Cloudflare DNS
        run: |
          echo "🌐 DNS records are managed in Cloudflare dashboard"
          echo "   or via Terraform (coming soon)"

  # Phase 5: Verify All Deployments
  verify-deployments:
    runs-on: ubuntu-latest
    needs: [update-dns]

    steps:
      - uses: actions/checkout@v4

      - name: Test All Endpoints
        run: |
          echo "🧪 Testing all endpoints..."

          # Test Cloudflare Pages
          echo "Testing Cloudflare Pages..."
          curl -sI https://blackroad.network | grep "200\|301\|302" || echo "⚠️ blackroad.network"
          curl -sI https://blackroad.systems | grep "200\|301\|302" || echo "⚠️ blackroad.systems"

          # Test Railway Services (when URLs are configured)
          echo "Testing Railway services..."
          # curl -sI https://api.blackroad.io/health | grep "200" || echo "⚠️ API Gateway"

          # Test Droplet
          echo "Testing Droplet services..."
          # curl -sI https://codex.blackroad.io/health | grep "200" || echo "⚠️ Codex"

          echo "✅ Verification complete!"

  # Send deployment notification
  notify:
    runs-on: ubuntu-latest
    needs: [verify-deployments]
    if: always()

    steps:
      - name: Deployment Summary
        run: |
          echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
          echo "🚀 MULTI-CLOUD DEPLOYMENT COMPLETE"
          echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
          echo ""
          echo "📦 Deployed to:"
          echo "   ✅ Cloudflare Pages (11 static sites)"
          echo "   ✅ Railway (Backend services)"
          echo "   ✅ DigitalOcean Droplet (Custom services)"
          echo ""
          echo "🌐 Platform Status:"
          echo "   Cloudflare: ${{ needs.deploy-cloudflare.result }}"
          echo "   Railway: ${{ needs.deploy-railway.result }}"
          echo "   Droplet: ${{ needs.deploy-droplet.result }}"
          echo ""
          echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
