from pseudo import Session, detect_names, detect_patterns, luhn_ok, iban_ok, pseudonymise, restore, show_card

TEXT = ("Hi, this is Maria Keller from Northwind Logistics. Please refund card 4539 1488 0343 6467 "
        "and send the confirmation to maria.keller@example.com or call +44 20 7946 0958. "
        "Our IBAN is GB82 WEST 1234 5698 7654 32. Order 1234 5678 9012 3456 is unrelated. "
        "Ms Keller will sign.")

print("checks:", luhn_ok("4539148803436467"), luhn_ok("1234567890123456"), iban_ok("GB82 WEST 1234 5698 7654 32"))

s1 = Session()
print("\nREGEX ONLY\n" + pseudonymise(TEXT, s1, [detect_patterns]))

s2 = Session()
known = {"Maria Keller": "PERSON", "Keller": "PERSON", "Northwind Logistics": "COMPANY"}
masked = pseudonymise(TEXT, s2, [detect_patterns, lambda t: detect_names(t, known)])
print("\nREGEX + KNOWN NAMES\n" + masked)

blob = s2.seal()
print("\nsealed table:", len(blob), "bytes")

answer = ("Refund issued to [CARD_1] for [person_1]; confirmation sent to (EMAIL_1). "
          "PERSON 1 can call [PHONE-1]. [PERSON_3] was not in the text.")
restored, report = restore(answer, Session.unseal(blob, s2.key), display={"CARD": show_card})
print("\nMODEL ANSWER\n" + answer + "\n\nRESTORED\n" + restored)
for line in report:
    print(" -", line)

try:
    Session.unseal(blob, Session().key)
except Exception as e:
    print("\nwrong key:", type(e).__name__)
