name: Deploy Explorer Live

on:
  workflow_dispatch:
  push:
    branches: [main, master]
    paths:
      - '.gitea/workflows/deploy-live.yml'
      - 'backend/**'
      - 'config/**'
      - 'deployment/**'
      - 'docs/**'
      - 'frontend/**'
      - 'scripts/**'
      - 'package.json'
      - 'package-lock.json'
      - 'Makefile'

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

      - name: Validate live deploy assets
        run: |
          test -f scripts/deploy-explorer-config-to-vmid5000.sh
          test -f scripts/deploy-explorer-ai-to-vmid5000.sh
          test -f scripts/deploy-next-frontend-to-vmid5000.sh
          test -f deployment/LIVE_DEPLOYMENT_MAP.md

      - name: Trigger explorer-live deployment
        run: |
          SHA="$(git rev-parse HEAD)"
          BRANCH="${GITHUB_REF_NAME:-}"
          if [ -z "$BRANCH" ] || [ "$BRANCH" = "HEAD" ]; then
            BRANCH="$(git rev-parse --abbrev-ref HEAD)"
          fi
          curl -sSf --connect-timeout 10 --max-time 3600 \
            -X POST "${{ secrets.PHOENIX_DEPLOY_URL }}" \
            -H "Authorization: Bearer ${{ secrets.PHOENIX_DEPLOY_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d "{\"repo\":\"${{ gitea.repository }}\",\"sha\":\"${SHA}\",\"branch\":\"${BRANCH}\",\"target\":\"explorer-live\"}"
