#!/usr/bin/env python3
"""Data quality pipeline: Ingest, profile, and validate data.

Demonstrates:
1. Data profiling (statistics per column).
2. Quality gates (completeness, bounds, types).
3. Data lineage tracking.
"""

from orxaq.orchestration.integration import OrxaqEngine

engine = OrxaqEngine()
engine.load_credit_ontology()
data = engine.generate_synthetic(n=500, seed=42)

# --- Data Profiling ---
print("=== Data Profile ===")
profile = engine.profile_data(data)
print(f"Rows: {profile.row_count}, Columns: {profile.column_count}")
for name, col in list(profile.columns.items())[:5]:
    if col.dtype == "float" or col.dtype == "numeric":
        print(f"  {name}: mean={col.mean:.4f}, std={col.std:.4f}")
    else:
        print(f"  {name}: type={col.dtype}, unique={col.unique_count}")

# --- Quality Gates ---
print("\n=== Quality Gates ===")
result = engine.run_quality_gates(data)
status = "PASSED" if result.all_passed else "FAILED"
print(f"Overall: {status} (score={result.overall_score:.2f})")
for gr in result.gate_results:
    s = "PASS" if gr.passed else "FAIL"
    print(f"  {gr.gate_name}: {s} (score={gr.score:.2f}, failures={len(gr.failures)})")

# --- Audit Trail ---
print("\n=== Recent Audit Entries ===")
entries = engine.audit_log.entries()[-5:]
for entry in entries:
    print(f"  [{entry.timestamp}] {entry.action} by {entry.actor}")
