import re

FILE_PATH = "c:/Users/Daivid.alves/Desktop/Repositorios/PlatformSistemas/Sistema_zentulo_analise/Codigo/analysis_src/zentulo_index.js"

patterns = {
    "FISCAL_XML": [
        r"window\.electronAPI\.processFiscal",
        r"DOMParser",
        r"<placa>",
        r"<vNF>",
        r"<vtPrest>"
    ],
    "OUTLOOK_EMAIL": [
        r"outlook-filter",
        r"socket\.on\(['\"]outlook",
        r"OutlookAutomation"
    ],
    "MONITORING": [
        r"socket\.on\(['\"]update-request",
        r"socket\.emit\(['\"]update-status",
        r"PENDING",
        r"APPROVED",
        r"EM_ROTA"
    ],
    "CHECKLIST": [
        r"getStatusFromExpiry",
        r"renovar",
        r"nextOS",
        r"ChecklistClient"
    ]
}

def extract_context(content, pattern_name, regex_list):
    print(f"\n{'='*20} {pattern_name} {'='*20}")
    for regex in regex_list:
        print(f"\n--- Searching for: {regex} ---")
        matches = list(re.finditer(regex, content, re.IGNORECASE))
        print(f"Found {len(matches)} matches.")
        
        for i, match in enumerate(matches[:3]): # Limit to first 3 matches per pattern
            start = max(0, match.start() - 300)
            end = min(len(content), match.end() + 300)
            snippet = content[start:end].replace('\n', ' ')
            print(f"[{i+1}] ...{snippet}...")

try:
    with open(FILE_PATH, 'r', encoding='utf-8', errors='ignore') as f:
        content = f.read()
    
    for key, regexes in patterns.items():
        extract_context(content, key, regexes)

except Exception as e:
    print(f"Error: {e}")
