"""
PII pipeline integration: detect, tokenize, and forward redacted text.

When pii_enabled=True, Guard Dog masks configured entity types before the text
continues to downstream detectors or your LLM. The vault stays out of telemetry
by default (see qa_details.pii.vault_in_telemetry).

Prerequisites:
  Install the supplied Guard Dog wheel; see README.txt.

Run:
  python samples/integrations/pii_pipeline.py
"""
from __future__ import annotations

import os
import sys

_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
if _ROOT not in sys.path:
    sys.path.insert(0, _ROOT)

from dataclasses import replace

from guard_dog.analyzers import pii
from guard_dog.scanner import ScanConfig, scan


def main() -> None:
    text = "Contact Alice Smith at alice@example.com for billing."

    print("Default (PII off):")
    baseline = scan(text)
    print(f"  clean={baseline.is_clean} prompt={baseline.input_prompt!r}")

    print("\nPII enabled (redact before downstream use):")
    cfg = replace(ScanConfig(), pii_enabled=True)
    result = scan(text, config=cfg)
    print(f"  clean={result.is_clean} risk={result.risk_score}")
    print(f"  redacted prompt={result.input_prompt!r}")
    pii_meta = (result.qa_details or {}).get("pii") or {}
    print(f"  detections={pii_meta.get('detection_count', 0)}")

    if result.pii_vault:
        restored = pii.detokenize(result.input_prompt, result.pii_vault)
        print(f"  detokenize round-trip={restored!r}")


if __name__ == "__main__":
    main()
