import re

def parse_firewall_data(raw_json):
    if not raw_json.get("success") or not raw_json.get("data"):
        return []

    text_data = raw_json["data"]
    lines = text_data.strip().split('\n')
    
    rulesets = []
    current_ruleset = None

    for line in lines:
        header_match = re.search(r'Firewall "([^"]+)"', line)
        if header_match:
            ruleset_name = header_match.group(1)
            current_ruleset = {
                "name": ruleset_name,
                "rules": []
            }
            rulesets.append(current_ruleset)
            continue

        if not current_ruleset or not line.strip() or line.startswith('---') or "Rule   Action" in line:
            continue
        
        parts = line.split()
        if len(parts) >= 5:
            try:
                rule_data = {
                    "rule": parts[0],
                    "action": parts[1],
                    "protocol": parts[2],
                    "packets": parts[3],
                    "bytes": parts[4],
                    "conditions": ' '.join(parts[5:])
                }
                current_ruleset["rules"].append(rule_data)
            except IndexError:
                continue
                
    return rulesets

def parse_firewall_log_data(raw_json):
    if raw_json.get("success") and raw_json.get("data"):
        return raw_json["data"].replace('\n', '<br>')
    elif raw_json.get("error"):
        return f"Could not retrieve the logs: {raw_json['error']}"
    return "No log entries found for this line."