# ═══════════════════════════════════════════════
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
# 🌙 天眼 · 每日系统休眠
# 每天凌晨 03:50 CST 启动预评估 → 04:00 进入日休眠
# 指令编号: ZY-HIBERNATION-2026-0324-001-A / 001-B

name: "🌙 天眼 · 每日系统休眠"

on:
  schedule:
    - cron: '50 19 * * *'  # UTC 19:50 = CST 03:50
  workflow_dispatch:
    inputs:
      force_duration:
        description: '强制休眠时长（分钟，0=天眼自动决定）'
        required: false
        default: '0'
  repository_dispatch:
    types: [emergency-wake]

permissions:
  contents: write

jobs:
  # ━━━ 紧急唤醒拦截 ━━━
  emergency-check:
    if: github.event_name == 'repository_dispatch' && github.event.action == 'emergency-wake'
    runs-on: ubuntu-latest
    steps:
      - name: "🚨 紧急唤醒"
        run: |
          echo "🚨 收到紧急唤醒指令"
          echo "签发者: ${{ github.event.client_payload.issued_by }}"
          echo "原因: ${{ github.event.client_payload.reason }}"
          if [[ "${{ github.event.client_payload.issued_by }}" != "TCS-0002" ]]; then
            echo "❌ 拒绝: 仅 TCS-0002 有权紧急唤醒"
            exit 1
          fi
          echo "✅ 紧急唤醒已确认 · 休眠流程将被跳过"

  # ━━━ 预评估 ━━━
  pre-evaluate:
    if: github.event_name != 'repository_dispatch'
    runs-on: ubuntu-latest
    outputs:
      planned_duration: ${{ steps.evaluate.outputs.sleep_minutes }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: "🧠 Evaluate Sleep Duration"
        id: evaluate
        run: |
          node skyeye/hibernation/sleep-scheduler.js --mode=daily
          echo "🧠 天眼评估完成"

      - name: "📢 Pre-announce"
        run: |
          node skyeye/hibernation/readme-status-updater.js \
            --phase=pre-announce \
            --mode=daily \
            --duration=${{ steps.evaluate.outputs.sleep_minutes || '12' }}

      - name: "Commit pre-announce"
        run: |
          git config user.name "天眼 · SkyEye Core"
          git config user.email "skyeye@guanghu.system"
          git add README.md
          git diff --cached --quiet || git commit -m "🌙 [skyeye] 日休眠预告 · $(date -u +%Y%m%d) [skip ci]"
          git push || true

  # ━━━ 执行日休眠 ━━━
  hibernate:
    if: github.event_name != 'repository_dispatch'
    needs: pre-evaluate
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: main

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: "⏸️ Enter Hibernation"
        run: |
          node skyeye/hibernation/readme-status-updater.js \
            --phase=hibernating --mode=daily
          echo "⏸️ 系统进入日休眠"

      - name: "🔍 Self-Check + Optimize + Archive"
        id: selfcheck
        run: |
          DURATION="${{ needs.pre-evaluate.outputs.planned_duration }}"
          node skyeye/hibernation/daily-hibernation.js \
            --planned-minutes=${DURATION:-12}

      - name: "⏯️ Resume"
        run: |
          node skyeye/hibernation/readme-status-updater.js \
            --phase=resumed --mode=daily

      - name: "Commit results"
        run: |
          git config user.name "天眼 · SkyEye Core"
          git config user.email "skyeye@guanghu.system"
          git add skyeye/ README.md
          git diff --cached --quiet || git commit -m "🌙 [skyeye] 日休眠完成 · $(date -u +%Y%m%d) [skip ci]"
          git push || true

      - name: "Reset to normal status"
        run: |
          sleep 5
          node skyeye/hibernation/readme-status-updater.js --phase=normal
          git add README.md
          git diff --cached --quiet || git commit -m "🌙 [skyeye] 系统状态恢复正常 [skip ci]"
          git push || true
