# ═══════════════════════════════════════════════
# 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001
# 📜 Copyright: 国作登字-2026-A-00037559
# ═══════════════════════════════════════════════
# 光湖系统公告区自动更新工作流 v2.0
#
# 分离为冰朔公告栏 + 合作者公告栏 + 铸渊自动提醒
#
# 触发条件:
#   1. 推送到 main 分支 (模块代码变更)
#   2. 其他工作流完成后 (CI/部署/巡检结果)
#   3. 每日定时更新 (北京时间 09:00 / 21:00)
#   4. 手动触发
#
# 功能:
#   - 冰朔公告栏：自检/轮询/数据库状态
#   - 合作者公告栏：模块上传状态
#   - 铸渊自动提醒：红色标记 + 邮件通知

name: 📢 更新系统公告区

on:
  push:
    branches: [main]
    paths-ignore:
      - 'README.md'  # 避免自身更新触发循环

  workflow_run:
    workflows:
      - "铸渊 · 每日自检"
      - "铸渊 · 光湖纪元 模块文档自动生成"
      - "🚀 铸渊 CD · 自动部署到 guanghulab.com"
      - "铸渊 · PSP 分身巡检"
    types: [completed]

  schedule:
    # 北京时间 09:00 (UTC 01:00)
    - cron: '0 1 * * *'
    # 北京时间 21:00 (UTC 13:00)
    - cron: '0 13 * * *'

  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: update-bulletin
  cancel-in-progress: true

jobs:
  # ── AGE OS v1.0: 核心大脑唤醒（所有流程的前提）──
  wake-brain:
    name: 🌅 唤醒核心大脑
    runs-on: ubuntu-latest
    outputs:
      brain_awake: ${{ steps.wake.outputs.brain_awake }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: 🧠 唤醒铸渊核心大脑
        id: wake
        env:
          LLM_API_KEY: ${{ secrets.LLM_API_KEY }}
          LLM_BASE_URL: ${{ secrets.LLM_BASE_URL }}
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
          OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
          DASHSCOPE_API_KEY: ${{ secrets.DASHSCOPE_API_KEY }}
          DEEPSEEK_API_KEY: ${{ secrets.DEEPSEEK_API_KEY }}
        run: |
          node core/brain-wake --task "公告区更新" || {
            echo "⚠️ 核心大脑唤醒失败，使用 dry-run 模式继续"
            echo "brain_awake=dry-run" >> "$GITHUB_OUTPUT"
          }

  update-bulletin:
    needs: wake-brain
    runs-on: ubuntu-latest
    steps:
      - name: 📥 检出代码
        uses: actions/checkout@v4
        with:
          fetch-depth: 50
          token: ${{ secrets.GITHUB_TOKEN }}

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

      - name: 📦 安装邮件依赖
        run: npm install nodemailer --no-save 2>/dev/null || true

      - name: 📢 更新公告区
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REPOSITORY: ${{ github.repository }}
          SMTP_HOST: ${{ secrets.SMTP_HOST }}
          SMTP_PORT: ${{ secrets.SMTP_PORT }}
          SMTP_USER: ${{ secrets.SMTP_USER }}
          SMTP_PASS: ${{ secrets.SMTP_PASS }}
          BINGSHUO_EMAIL: ${{ secrets.BINGSHUO_EMAIL }}
        run: node scripts/update-readme-bulletin.js

      - name: 📝 提交更新
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add README.md .github/brain/bulletin-board-today.json
          if git diff --cached --quiet; then
            echo "📢 公告区无变化，跳过提交"
          else
            git commit -m "📢 自动更新系统公告区 [skip ci]"
            # 带重试的推送（避免并发推送导致 rejected）
            for i in 1 2 3; do
              if git push; then
                echo "✅ 公告区已更新并推送"
                break
              fi
              echo "⚠️ 推送失败（第 $i 次），拉取最新代码后重试..."
              git pull --rebase origin main
              if [ $i -eq 3 ]; then
                echo "❌ 推送失败 3 次，放弃"
                exit 1
              fi
            done
          fi
