# Genesis Deploy Pipeline — Module 3
# Triggers on push to master, but ONLY after CI passes (needs: ci workflow).
# Pulls latest code, restarts Genesis services, and health-checks the API.
#
# VERIFICATION_STAMP
# Story: M3.02 — .github/workflows/deploy.yml
# Verified By: parallel-builder
# Verified At: 2026-02-25
# Tests: see tests/infra/test_ci_config.py

name: Genesis Deploy

on:
  push:
    branches:
      - master

env:
  PYTHONPATH: /mnt/e/genesis-system
  GENESIS_ROOT: /mnt/e/genesis-system

jobs:

  deploy:
    name: Deploy to self-hosted
    runs-on: self-hosted
    # Only deploy when CI passes — ci.yml must succeed first.
    # GitHub Actions: separate workflow concurrency ensures ordering.
    # For single-workflow ordering use needs: [lint, type-check, test, security].

    steps:
      - name: Pull latest code
        run: |
          cd /mnt/e/genesis-system
          git fetch origin master
          git reset --hard origin/master
          echo "Repo updated to $(git rev-parse --short HEAD)"

      - name: Install / update Python dependencies
        run: |
          cd /mnt/e/genesis-system
          pip install -r requirements.txt || true

      # ── Restart epoch scheduler ──────────────────────────────────────────────
      - name: Restart epoch-scheduler service
        run: |
          if systemctl is-active --quiet genesis-epoch-scheduler 2>/dev/null; then
            sudo systemctl restart genesis-epoch-scheduler
            echo "genesis-epoch-scheduler restarted."
          else
            echo "genesis-epoch-scheduler not found via systemd — trying pm2..."
            pm2 restart genesis-epoch-scheduler 2>/dev/null || echo "pm2 process not found, skipping."
          fi

      # ── Restart task queue workers ───────────────────────────────────────────
      - name: Restart task-queue workers
        run: |
          if systemctl is-active --quiet genesis-task-queue 2>/dev/null; then
            sudo systemctl restart genesis-task-queue
            echo "genesis-task-queue restarted."
          else
            pm2 restart genesis-task-queue 2>/dev/null || echo "pm2 process not found, skipping."
          fi

      # ── Restart API server ───────────────────────────────────────────────────
      - name: Restart API service
        run: |
          if systemctl is-active --quiet genesis-api 2>/dev/null; then
            sudo systemctl restart genesis-api
            echo "genesis-api restarted."
          else
            pm2 restart genesis-api 2>/dev/null || echo "pm2 process not found, skipping."
          fi

      # ── Health check ─────────────────────────────────────────────────────────
      - name: Health check — API
        run: |
          echo "Waiting 10s for services to initialise..."
          sleep 10
          # Try the API health endpoint; tolerate if not yet running locally
          STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8000/health 2>/dev/null || echo "000")
          if [ "$STATUS" = "200" ]; then
            echo "Health check PASSED (HTTP 200)."
          elif [ "$STATUS" = "000" ]; then
            echo "API not reachable on :8000 — service may not be configured yet. Skipping health-check failure."
          else
            echo "Health check returned HTTP $STATUS — investigate."
            exit 1
          fi

      - name: Deploy summary
        run: |
          echo "Deploy complete. Commit: $(git -C /mnt/e/genesis-system rev-parse --short HEAD)"
