#!/usr/bin/env python3
from __future__ import annotations
"""
Superglyph Integration Check
----------------------------
Quick verification that the Superglyph architecture injection is working.

Usage:
    PYTHONPATH=. python3 -m helix_cdc.check_superglyph_integration
    PYTHONPATH=. python3 -m helix_cdc.check_superglyph_integration /tmp/pheromones.jsonl
"""

import json
import sys
from pathlib import Path
from typing import Optional, List

from helix_cdc.superglyph_bridge import dump_superglyph_summary, describe_superglyph_substrate
from helix_cdc.bloomos.substrate_inspector import analyze_substrate
from helix_cdc.bloomos.sym import SymCapsule, sym_capsule


def main(argv: Optional[List[str]] = None) -> int:
    args = argv if argv is not None else sys.argv[1:]
    pheromone_path = Path(args[0]) if args else Path("/tmp/test_pheromones_with_goals.jsonl")

    print("=" * 70)
    print("SUPERGLYPH INTEGRATION CHECK")
    print("=" * 70)
    print()

    # 1. Check Superglyph bridge
    print("=== 1. Superglyph Substrate Descriptor ===")
    desc = describe_superglyph_substrate()
    print(f"  Name:    {desc.name}")
    print(f"  Version: {desc.version}")
    print()
    print("  Capabilities:")
    caps = desc.capabilities
    print(f"    has_genome:        {caps.has_genome}")
    print(f"    has_lab_tools:     {caps.has_lab_tools}")
    print(f"    has_agentic_cells: {caps.has_agentic_cells}")
    print(f"    has_swarm_scripts: {caps.has_swarm_scripts}")
    print(f"    has_regen_core:    {caps.has_regen_core}")
    print()
    if desc.genome.path:
        print(f"  Genome Path: {desc.genome.path}")
        print(f"  Genome Size: {desc.genome.size_bytes} bytes")
    print()

    # 2. Check SymCapsule substrate fields
    print("=== 2. SymCapsule Substrate Metadata ===")
    capsule = sym_capsule(
        data={"test": "integration"},
        substrate="superglyph",
        engine="Superglyph/AgenticCell",
        engine_meta={"genome": desc.genome.path, "lobe": "test_lobe"},
    )
    print(f"  substrate:   {capsule.substrate}")
    print(f"  engine:      {capsule.engine}")
    print(f"  engine_meta: {capsule.engine_meta}")
    print()

    # 3. Check substrate_inspector integration
    print("=== 3. Substrate Inspector Integration ===")
    if pheromone_path.exists():
        print(f"  Analyzing: {pheromone_path}")
        summary = analyze_substrate(str(pheromone_path))
        print(f"  Total entries:    {summary.total_entries}")
        print(f"  Task types:       {summary.unique_task_types}")
        print()
        sg = summary.superglyph
        print("  Superglyph in summary:")
        print(f"    name:    {sg.get('name')}")
        print(f"    version: {sg.get('version')}")
        sg_caps = sg.get("capabilities", {})
        all_present = all([
            sg_caps.get("has_genome"),
            sg_caps.get("has_lab_tools"),
            sg_caps.get("has_agentic_cells"),
        ])
        print(f"    all_capabilities_present: {all_present}")
    else:
        print(f"  (No pheromone file at {pheromone_path}, skipping substrate analysis)")
    print()

    # Summary
    print("=" * 70)
    print("INTEGRATION STATUS")
    print("=" * 70)
    checks = [
        ("Superglyph bridge import", True),
        ("Superglyph descriptor", desc.name is not None),
        ("SymCapsule substrate field", capsule.substrate == "superglyph"),
        ("SymCapsule engine field", capsule.engine == "Superglyph/AgenticCell"),
        ("Genome detected", caps.has_genome),
        ("Agentic cells detected", caps.has_agentic_cells),
        ("Regen core detected", caps.has_regen_core),
    ]

    all_pass = True
    for name, passed in checks:
        status = "PASS" if passed else "FAIL"
        if not passed:
            all_pass = False
        print(f"  [{status}] {name}")

    print()
    if all_pass:
        print("All checks passed. Superglyph architecture injection complete.")
    else:
        print("Some checks failed. Review output above.")

    return 0 if all_pass else 1


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