# -*- coding: utf-8 -*-
"""
yjk_boot.py —— 一句成课™ R2 启动编排器（明文外壳）
学员激活后由引擎/安装器调用：
  1) require_license() 网关（无 license.json 立即拒绝）
  2) 解密 SKILL.md.vault → 运行时缓存（引擎读取的真正指令）
  3) 解密 assets/*.md.vault → 运行时缓存（方法论）
  4) 解密 scripts/*.py.vault、*.js.vault → 运行时缓存（引擎代码）
解密内容仅在本人目录缓存、仅本人可读写；断网/换设备则密钥失效需重激活。
"""
import os, sys, json
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from yjk_crypto import require_license, license_status
import decrypt_assets as da

ROOT = da.ROOT
RUNTIME = da.RUNTIME

def boot() -> dict:
    st = require_license()  # 网关：未激活直接抛 PermissionError
    os.makedirs(RUNTIME, exist_ok=True)
    report = {"activated": True, "decrypted": []}
    # 1) SKILL.md
    skill_rel = os.path.join('SKILL MD', 'SKILL.md')
    try:
        da.ensure_runtime(skill_rel)
        report["decrypted"].append(skill_rel)
    except Exception as e:
        report.setdefault("errors", []).append(f"{skill_rel}: {e}")
    # 2) 遍历 manifest 解密其余资产
    for v in da.list_vaults():
        rel = v.get('rel_no_ext') or v.get('orig_rel')
        if not rel:
            continue
        if rel.replace('\\', '/') == skill_rel.replace('\\', '/'):
            continue
        try:
            da.ensure_runtime(rel)
            report["decrypted"].append(rel)
        except Exception as e:
            report.setdefault("errors", []).append(f"{rel}: {e}")
    return report

if __name__ == '__main__':
    try:
        r = boot()
        print("✅ 启动解密完成：", len(r["decrypted"]), "个资产")
        for d in r["decrypted"][:8]:
            print("  ✓", d)
        if r.get("errors"):
            print("⚠ 部分失败：")
            for e in r["errors"]:
                print("  ✗", e)
    except PermissionError as e:
        print("⛔", e)
        sys.exit(2)
