#!/usr/bin/env python3
"""
Phase 3 Ingestion Orchestrator.

Runs all Phase 3 pipelines in sequence:
1. System hardware & services
"""

import sys
import subprocess
from pathlib import Path

WIKI_DIR = Path(__file__).parent


def run_pipeline(name, script):
    """Run a single ingestion pipeline."""
    print(f"\n{'='*60}")
    print(f"  {name}")
    print(f"{'='*60}")
    
    script_path = WIKI_DIR / script
    if not script_path.exists():
        print(f"ERROR: Script not found: {script_path}")
        return False
    
    result = subprocess.run(
        [sys.executable, str(script_path)],
        cwd=str(WIKI_DIR),
        capture_output=False,
        timeout=60,
    )
    
    if result.returncode != 0:
        print(f"\nāŒ Pipeline failed with exit code {result.returncode}")
        return False
    
    print(f"\nāœ“ Pipeline completed")
    return True


def verify_brain_state():
    """Verify final brain state after all pipelines."""
    import sqlite3
    
    db = sqlite3.connect(str(Path.home() / '.hermes' / 'brain' / 'index' / 'events.db'))
    rows = db.execute('SELECT tool, COUNT(*) as cnt FROM events GROUP BY tool ORDER BY cnt DESC').fetchall()
    total = db.execute('SELECT COUNT(*) as cnt FROM events').fetchone()[0]
    db.close()
    
    print("\n" + "="*60)
    print("FINAL BRAIN STATE")
    print("="*60)
    print(f"Total events: {total}")
    print(f"\nEvents by tool:")
    for row in rows:
        print(f"  {row[0]}: {row[1]}")


if __name__ == '__main__':
    print("="*60)
    print("PHASE 3 INGESTION — System Monitoring")
    print("="*60)
    
    pipelines = [
        ("System Hardware & Services", "system_brain_ingest.py"),
    ]
    
    results = []
    for name, script in pipelines:
        success = run_pipeline(name, script)
        results.append((name, success))
    
    # Summary
    print("\n" + "="*60)
    print("PHASE 3 SUMMARY")
    print("="*60)
    
    for name, success in results:
        status = "āœ“" if success else "āŒ"
        print(f"  {status} {name}")
    
    # Final state
    verify_brain_state()
    
    all_success = all(s for _, s in results)
    sys.exit(0 if all_success else 1)