#!/usr/bin/env python3
"""self_todo_executor.py — TODO自动执行器 (no_agent模式)

每10分钟轮询pending TODOs，执行fix_action。
成功→completed。失败→调gateway API，带完整上下文让知微处理。
"""

import json, subprocess, time, urllib.request
from pathlib import Path
from datetime import datetime
from mofin_db import get_conn

BASE = Path("/home/hmo/MoFin")
DB_PATH = BASE / "data" / "mofin.db"
GATEWAY_URL = "http://localhost:8643/v1/chat/completions"
GATEWAY_KEY = "hermes123"


def send_xmpp(msg):
    """通过zhiwei发XMPP消息给Dad"""
    try:
        subprocess.run(
            ["hermes", "send", "--to", "xmpp:hmo@yoin.fun", msg],
            capture_output=True, text=True, timeout=15
        )
    except:
        pass


def main():
    start = time.time()
    conn = get_conn()

    rows = conn.execute(
        "SELECT id, title, description, fix_action FROM todos WHERE status='pending' "
        "ORDER BY CASE priority WHEN 'high' THEN 0 WHEN 'medium' THEN 1 ELSE 2 END, "
        "created_at ASC LIMIT 3"
    ).fetchall()

    if not rows:
        conn.close()
        print("[SILENT] 无待处理TODO")
        return

    for row in rows:
        tid = row["id"]
        title = row["title"]
        desc = row["description"] or ""
        fix = row["fix_action"]

        conn.execute("UPDATE todos SET status='in_progress' WHERE id=?", (tid,))
        conn.commit()

        if not fix:
            # 无修复方案 → 带完整描述调gateway
            context = f"[自愈执行器] 系统体检发现以下问题，无自动修复方案，需分析处理。\n\n问题: {title}\n\n详情:\n{desc}".strip()
            send_xmpp(f"📋 TODO已创建(无自动修复): {title[:80]}")
        else:
            # 执行修复命令
            try:
                r = subprocess.run(fix, shell=True, capture_output=True, text=True, timeout=30)
                if r.returncode == 0:
                    conn.execute("UPDATE todos SET status='completed', note=? WHERE id=?",
                               (f"已修复: {r.stdout.strip()[:200]}", tid))
                    conn.commit()
                    send_xmpp(f"✅ TODO修复成功: {title[:80]}")
                    print(f"  ✅ {title}: 已修复")
                    continue
                output = r.stderr.strip()[:500] or r.stdout.strip()[:500]
            except subprocess.TimeoutExpired:
                output = "执行超时(30s)"
            except Exception as e:
                output = str(e)[:200]

            context = (
                f"[自愈执行器] 尝试自动修复失败，需知微分析处理。\n\n"
                f"问题: {title}\n\n"
                f"详情:\n{desc}\n\n"
                f"尝试的修复命令: {fix}\n"
                f"失败输出: {output}\n\n"
                f"请分析失败原因并完成修复，完成后标记TODO #{tid} 为 completed。"
            )

        # 调gateway让知微处理（带完整上下文）
        payload = json.dumps({
            "model": "default",
            "messages": [{"role": "user", "content": context}],
            "max_tokens": 1000,
        }).encode()
        req = urllib.request.Request(GATEWAY_URL, data=payload,
            headers={"Content-Type": "application/json",
                     "Authorization": f"Bearer {GATEWAY_KEY}"})
        try:
            resp = urllib.request.urlopen(req, timeout=120)
            reply = json.loads(resp.read())
            result = reply["choices"][0]["message"]["content"][:500]
            conn.execute("UPDATE todos SET status='completed', note=? WHERE id=?",
                       (f"知微已处理: {result[:200]}", tid))
            send_xmpp(f"🔶 TODO需知微处理: {title[:60]}\n{result[:200]}")
            print(f"  🔶 {title}")
            print(f"     {result[:300]}")
        except Exception as e:
            conn.execute("UPDATE todos SET status='pending', note=? WHERE id=?",
                       (f"调用知微失败: {str(e)[:100]}，下次再试", tid))
            send_xmpp(f"⚠️ TODO处理失败(将重试): {title[:60]}\n{str(e)[:100]}")
            print(f"  ⚠️ {title}: gateway API调用失败，下次再试")

        conn.commit()

    conn.close()

    if rows:
        print(f"自愈执行器 | {datetime.now().strftime('%H:%M')} | {len(rows)}条 ({time.time()-start:.0f}s)")
    else:
        print("[SILENT] 无待处理TODO")


if __name__ == "__main__":
    main()
