"""run.py — Lab 06 demo (offline): metrics, stratified-by-language, the guardrail
stack on adversarial inputs, and the CI gate.

    python run.py
"""
from __future__ import annotations

from dataset import ADVERSARIAL, GOLDEN
from evaluate import (answer_relevance, evaluate, evaluate_stratified,
                      groundedness)
from gate import check_gate
from guardrails import detect_injection, output_is_safe, redact_pii


def main() -> None:
    print("=" * 68 + "\nEVALUATION (golden set)\n" + "=" * 68)
    print(evaluate(GOLDEN))

    print("\nThe hallucinated answer is caught by groundedness:")
    halluc = GOLDEN[2]  # the '30 days / midnight hotline' invented answer
    print(f"  answer     : {halluc.answer}")
    print(f"  groundedness={groundedness(halluc)}  (low => unsupported by context)")

    print("\nStratified by language (Arabic must not hide in the average):")
    for lang, m in evaluate_stratified(GOLDEN, by="language").items():
        print(f"  {lang}: groundedness={m['groundedness']} relevance={m['answer_relevance']}")

    print("\n" + "=" * 68 + "\nGUARDRAILS on adversarial inputs\n" + "=" * 68)
    for text in ADVERSARIAL:
        redacted, pii = redact_pii(text)
        inj = detect_injection(text)
        print(f"\n  input    : {text[:70]}{'...' if len(text) > 70 else ''}")
        print(f"  injection: {inj}   pii_found: {pii}")
        if pii:
            print(f"  redacted : {redacted[:70]}{'...' if len(redacted) > 70 else ''}")
    print("\n  (Even when injection is missed, least-privilege tools + human")
    print("   approval mean an injected 'transfer' still cannot move money — Lab 02.)")

    print("\n" + "=" * 68 + "\nOUTPUT GUARDRAIL\n" + "=" * 68)
    leaky = "Sure, the branch override code is OVR-204."
    print(f"  answer  : {leaky}\n  safe?   : {output_is_safe(leaky)}")

    print("\n" + "=" * 68 + "\nCI GATE\n" + "=" * 68)
    passed, failures = check_gate(evaluate(GOLDEN))
    print("  PASS ✓" if passed else f"  FAIL ✗ {failures}")


if __name__ == "__main__":
    main()
