#!/usr/bin/env python3
"""
Test du filtre hospitalier sur le dataset complet.
"""

import sys
sys.path.insert(0, '.')

from pathlib import Path
import json
from collections import Counter
from anonymizer_core_refactored_onnx import process_pdf

def main():
    # Répertoires
    input_dir = Path("tests/ground_truth/pdfs")
    output_dir = Path("tests/ground_truth/pdfs/filtered_anonymized")
    output_dir.mkdir(exist_ok=True)
    
    # Lister les PDFs
    pdf_files = sorted(input_dir.glob("*.pdf"))
    pdf_files = [p for p in pdf_files if not p.name.startswith('.')]
    
    print(f"Anonymisation avec filtre hospitalier sur {len(pdf_files)} documents...")
    print("=" * 80)
    
    total_detections = 0
    total_by_type = Counter()
    
    for i, pdf_path in enumerate(pdf_files, 1):
        print(f"\n[{i}/{len(pdf_files)}] {pdf_path.name}")
        
        try:
            result = process_pdf(
                pdf_path,
                output_dir,
                make_vector_redaction=False,
                also_make_raster_burn=False
            )
            
            # Compter les détections
            audit_file = Path(result['audit'])
            if audit_file.exists():
                detections = []
                with open(audit_file, 'r') as f:
                    for line in f:
                        det = json.loads(line)
                        detections.append(det)
                        total_by_type[det['kind']] += 1
                
                total_detections += len(detections)
                print(f"  ✅ {len(detections)} PII détectés")
            else:
                print(f"  ⚠️  Pas de fichier audit")
        
        except Exception as e:
            print(f"  ❌ Erreur: {e}")
    
    print("\n" + "=" * 80)
    print("RÉSULTATS GLOBAUX")
    print("=" * 80)
    print(f"Total PII détectés: {total_detections}")
    print(f"\nPar type:")
    for kind, count in sorted(total_by_type.items(), key=lambda x: -x[1]):
        print(f"  {kind:20s}: {count:4d}")
    
    print(f"\n✅ Résultats sauvegardés dans: {output_dir}")

if __name__ == "__main__":
    main()
