import re

class SecurityManager:
    """Enterprise DLP Security Manager & RBAC profile handler."""
    def __init__(self, filters=None):
        self.library = {
            "EMAIL": r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b',
            "PHONE": r'\b(?:\+|00|0)(?:[\s.\-\(\)]*\d){7,15}\b',
            "ID_NUM": r'\b\d{13,15}\b|\b\d{9}\b|\b\d{14}\b',
            "PASSPORT": r'\b[A-Z]{1,2}[0-9]{6,9}\b',
            "SSN": r'\b\d{3}-\d{2}-\d{4}\b',
            "ADDRESS": r'(?i)\b\d{1,5}[,\s]+(?:bis|ter|quat)?\s*(?:rue|avenue|av\.?|boulevard|bd|place|pl\.?|impasse|allée|quai|route|chemin|lane|street|road|drive|way|sq\.?|square)\s+[\w\s\-\']+',
            "POSTAL": r'(?i)(?:\b(?:BP|P\.O\.?\s*Box|CS|TSA)\s*\d+\b)|(?:\b\d{5}|\b\d{4}|\b[A-Z0-9]{3,7})\s+[A-Z][a-zA-Z\s\-]+',
            "BIC": r'\b[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?\b',
            "IBAN": r'\b[A-Z]{2}\d{2}[A-Z0-9]{4}\d{7}([A-Z0-9]?){0,16}\b',
            "VAT_ID": r'\b[A-Z]{2}\s*[0-9A-Z\.\-]{8,15}\b',
            "CREDIT_CARD": r'\b(?:\d[ -]*?){13,19}\b',
            "MONEY": r'\b\d{1,3}(?:[\s.,]\d{3})*(?:[\.,]\d+)?\s?(?:€|\$|£|EUR|USD|XAF|FCFA)\b',
            "CRYPTO": r'\b(0x[a-fA-F0-9]{40}|[13][a-km-zA-H-J-NP-Z1-9]{25,34})\b',
            "IP_ADDR": r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b',
            "MAC_ADDR": r'\b([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})\b',
            "API_KEY": r'\b((?:AKIA|ABIA|ACCA|ASIA)[0-9A-Z]{16})|(sk_live_[0-9a-zA-Z]{24,})|(ghp_[0-9a-zA-Z]{36})|([a-zA-Z0-9]{32,})\b',
            "DATE": r'\b\d{2}[/-]\d{2}[/-]\d{4}\b|\b\d{4}[/-]\d{2}[/-]\d{2}\b',
            "SALARY": r'(?i)\b(?:salary|salaire|wage|income|revenu)\s*:?\s*\d+[\s.,]*\d*\s*(?:€|\$|£|EUR|USD)?\b',
            "BIRTHDATE": r'(?i)\b(?:born|né|née|date of birth|date de naissance)\s*:?\s*\d{2}[/-]\d{2}[/-]\d{4}\b',
            "MEDICAL": r'(?i)\b(?:diagnosis|diagnostic|treatment|traitement|prescription|medication|médicament)\s*:?\s*[\w\s]+\b', 
        }

        self.friendly_messages = {k: f"[{k.replace('_', ' ').title()} Redacted]" for k in self.library.keys()}

        self.active_patterns = {}
        if filters is True: self.active_patterns = self.library
        elif isinstance(filters, list):
            for key in filters:
                key_upper = key.upper()
                if key_upper in self.library: self.active_patterns[key_upper] = self.library[key_upper]
        
        self.is_active = len(self.active_patterns) > 0

    def scrub(self, text, user_profile=None):
        """Clean text based on DLP and User Profile (RBAC)."""
        if not text: return text
        
        # Dynamic Profile Handling: If Admin/CEO, bypass filters
        if user_profile and str(user_profile).lower() in ["admin", "ceo", "root"]:
            return text
            
        if self.is_active:
            for label, regex in self.active_patterns.items():
                replacement = self.friendly_messages.get(label, "[Redacted]")
                text = re.sub(regex, replacement, text)

        return text