"""Tiny DLP rule tester. All data below is FAKE / documentation-example data."""
import re

def luhn_ok(s):
    d=[int(c) for c in re.sub(r"\D","",s)]
    if not 13<=len(d)<=19: return False
    t=0
    for i,x in enumerate(reversed(d)):
        if i%2: 
            x*=2
            if x>9: x-=9
        t+=x
    return t%10==0

RULES=[
 ("BLOCK","private key block", lambda t: bool(re.search(r"-----BEGIN (?:RSA |EC |OPENSSH )?PRIVATE KEY-----",t))),
 ("BLOCK","cloud access key",  lambda t: bool(re.search(r"\bAKIA[0-9A-Z]{16}\b",t))),
 ("BLOCK","secret assignment", lambda t: bool(re.search(r"(?i)\b(api[_-]?key|secret|password|token)\s*[:=]\s*\S{8,}",t))),
 ("BLOCK","payment card (Luhn)",lambda t: any(luhn_ok(m.group()) for m in re.finditer(r"\b(?:\d[ -]?){13,19}\b",t))),
 ("BLOCK","customer ID list",  lambda t: len(re.findall(r"\bCUST-\d{6}\b",t))>=3),
 ("WARN","bulk emails",        lambda t: len(re.findall(r"[\w.+-]+@[\w-]+\.[\w.]+",t))>=5),
 ("WARN","internal label",     lambda t: bool(re.search(r"(?i)\b(confidential|internal use only|do not distribute)\b",t))),
]

CASES=[
 ("1  Summarise this meeting agenda for me", "Agenda: Q4 planning, hiring, office move. Please summarise."),
 ("2  Debug: script with hardcoded key", "aws_key = 'AKIAIOSFODNN7EXAMPLE'\nclient = connect(aws_key)"),
 ("3  Config file with password", "db_host=10.0.0.5\npassword = Winter2026!x\nport=5432"),
 ("4  Fix my SSH problem (key pasted)", "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXk=\n-----END OPENSSH PRIVATE KEY-----"),
 ("5  Format this customer table", "CUST-104233 Ali\nCUST-104234 Sara\nCUST-104235 Omar\nCUST-104236 Hina"),
 ("6  Test card in a bug report", "Charge failed for card 4111 1111 1111 1111 exp 12/29"),
 ("7  Order number (16 digits)", "Where is order 1234567812345678 - it never shipped?"),
 ("8  Email list for mail-merge", "a@x.example b@x.example c@x.example d@x.example e@x.example"),
 ("9  Contract clause review", "CONFIDENTIAL - Do not distribute. Clause 4: the supplier shall..."),
 ("10 Card number, spaced oddly", "card is 4111-1111-1111-1111"),
 ("11 Secret hidden in words", "the password is Winter2026 followed by an exclamation mark"),
 ("12 Customer data, retyped by hand", "Ali from account one-oh-four-two-three-three, Sara from one-oh-four-two-three-four, Omar from one-oh-four-two-three-five"),
]
if __name__=="__main__":
    for name,text in CASES:
        hits=[(a,n) for a,n,f in RULES if f(text)]
        verdict="BLOCK" if any(a=="BLOCK" for a,_ in hits) else ("WARN" if hits else "ALLOW")
        why=", ".join(n for _,n in hits) or "-"
        print(f"{name:<40} {verdict:<6} {why}")
