#!/usr/bin/env python3
"""预发布审核通知 — 发布文章前推送企微群，等待人工审核"""
import json, sys, urllib.request

WEBHOOK = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d4de64e1-efaa-4f03-a833-ad85c1d6de5b"

def send_review_notice(articles: list[dict]) -> bool:
    """
    articles: [{"title": "...", "channel": "栏目名", "channel_id": 227, "url": "https://..."}]
    """
    lines = ["## 🔔 预发布审核通知"]
    lines.append(f"> 待审核文章：**{len(articles)} 篇**")
    lines.append("> 状态：<font color=\"warning\">待人工审核</font>\n")
    
    for i, a in enumerate(articles, 1):
        lines.append(f"**文章 {i}**")
        lines.append(f"- 标题：{a['title']}")
        lines.append(f"- 栏目：{a['channel']}（ID:{a['channel_id']}）")
        lines.append(f"- 链接：[点击预览]({a['url']})")
        lines.append("")
    
    lines.append("---")
    lines.append("请审核后回复「通过」或「驳回」")
    
    payload = json.dumps({
        "msgtype": "markdown",
        "markdown": {"content": "\n".join(lines)}
    }).encode()
    
    req = urllib.request.Request(WEBHOOK, data=payload, headers={"Content-Type": "application/json"})
    resp = json.loads(urllib.request.urlopen(req).read())
    return resp.get("errcode") == 0

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: pre_publish_review.py '<json_articles>'")
        sys.exit(1)
    
    articles = json.loads(sys.argv[1])
    ok = send_review_notice(articles)
    print("✅ 已推送" if ok else "❌ 推送失败")
    sys.exit(0 if ok else 1)
