#!/usr/bin/env python3
"""CorvOS v2.3-Omega - Sistema Completo"""
import sys, os, numpy as np, json, hashlib
sys.path.insert(0, os.path.dirname(__file__))
from corvos_main import *
from manifesto_2027 import Manifesto2027
from stress_simulation_7d import StressSimulation7Days

async def main():
    print('='*70)
    print('  CORVOS v2.3-OMEGA - SISTEMA COMPLETO')
    print('  Previsao Preditiva + Manifesto 2027 + Regeneracao + 7 Dias')
    print('='*70)
    print()
    c = CorvOSv2()
    await c.init()
    await c.deploy()
    print()
    print('='*70)
    print('  BIO-LINK MASS SYNCHRONIZATION - 40Hz')
    print('='*70)
    for pulse in range(13):
        result = await c.biolink.emit_pulse()
        aligned = result['aligned']
        total = aligned + result['drifting'] + result['lost']
        pct = 100*aligned/total if total>0 else 0
        bar = '='*int(pct/5) + '-'*(20-int(pct/5))
        print(f'  Pulso {pulse+1:02d}: [{bar}] {pct:.1f}% | gc={result["global_coherence"]:.3f}')
        await asyncio.sleep(0.02)
    report = c.biolink.get_sync_report()
    print()
    print(f'  Bio-Link: {report["total_residents"]} residents | gc={report["global_coherence"]:.4f}')
    print()
    # Generate Manifesto 2027
    evals = c.happiness.eigenvalues
    bio_report = {
        'telomere_length': 0.892,
        'oxidative_stress': 0.42,
        'mitochondrial_efficiency': 0.81,
        'inflammation_marker': 0.31,
        'overall_score': 0.794
    }
    manifesto_gen = Manifesto2027(
        coherence=0.999,
        spectral_radius=c.happiness.spectral_radius,
        bio_sync=report['global_coherence']
    )
    manifesto = manifesto_gen.generate_json(evals, bio_report)
    mpath = '/home/workspace/corvos_sys/manifesto_2027.json'
    with open(mpath, 'w') as f:
        json.dump(manifesto, f, indent=2, ensure_ascii=False)
    print('='*70)
    print('  MANIFESTO DE GOVERNANCA 2027')
    print('='*70)
    print(f"  ID: {manifesto['manifesto_id']}")
    print(f"  Versao: {manifesto['version']}")
    print(f"  Tipo: {manifesto['algorithmic_constitution']['economic_model']['type']}")
    print(f"  Moeda: {manifesto['algorithmic_constitution']['economic_model']['currency']}")
    print(f"  Lambda2: {manifesto['bio_metrics']['coherence_lambda2']}")
    print(f"  BioSync: {manifesto['bio_metrics']['population_sync_pct']}%")
    print(f"  Arkhe-Chain: {manifesto['arkhe_chain_ref']}")
    print()
    print('  Projecoes 2027:')
    for sec, data in manifesto['projections_2027'].items():
        print(f"    {sec:12s}: {data['current']:.3f} -> {data['projected_2027']:.3f} ({data['growth_rate_pct']:+.1f}%)")
    print()
    print(f"  Roadmap: {' | '.join([p['milestone'][:30] for p in manifesto['roadmap']])}")
    print()
    print('  JSON exportado para Arkhe-Chain: manifesto_2027.json')
    print()
    # 7-Day stress simulation
    sim = StressSimulation7Days(c)
    await sim.run()
    print()
    print('='*70)
    print('  CORVOS v2.3-OMEGA - OPERACIONAL')
    print('='*70)

if __name__ == '__main__':
    asyncio.run(main())