#!/usr/bin/env python3
from __future__ import annotations

import json
from pathlib import Path


def find_package_root() -> Path:
    here = Path(__file__).resolve()
    for parent in here.parents:
        if (parent / "manifest.json").exists() and (parent / "runtime-tools" / "tool-manifest.json").exists():
            return parent
    raise SystemExit("AIOS package root not found")


def main() -> int:
    root = find_package_root()
    tool_manifest = json.loads((root / "runtime-tools" / "tool-manifest.json").read_text(encoding="utf-8"))
    skill_tool_map = json.loads((root / "bindings" / "skill-tool-map.json").read_text(encoding="utf-8"))
    missing = []
    for tool in tool_manifest.get("tools", []):
        for rel in tool.get("files", []):
            if not (root / rel).exists():
                missing.append(rel)
    print(json.dumps({
        "ok": not missing,
        "tools": len(tool_manifest.get("tools", [])),
        "skill_bindings": len(skill_tool_map.get("skills", {})),
        "missing": missing,
    }, ensure_ascii=False))
    return 0 if not missing else 1


if __name__ == "__main__":
    raise SystemExit(main())
