from tokenize import group


network_groups = {
    "FQDN_GROUP": {
        "name": "FQDN_GROUP",
        "description": "123",
        "members": [
            {
                "type": "object",
                "net_object": "FQDN_OBJ"
            }
        ]
    },
    "ds_group_1": {
        "name": "ds_group_1",
        "description": "some description",
        "members": [
            {
                "net_object": "2001:db8:85a3::",
                "prefix_length": "65"
            },
            {
                "type": "172.31.142.0",
                "host": "255.255.255.0"
            },
            {
                "type": "object",
                "net_object": "ds_object_fqdn_v4"
            },
            {
                "type": "object",
                "net_object": "ds_object_network_v4"
            },
            {
                "type": "object",
                "net_object": "tetetet"
            },
            {
                "type": "group",
                "grp_object": "FQDN_GROUP"
            }
        ]
    },
    "net_group1": {
        "name": "net_group1",
        "description": "",
        "members": [
            {
                "type": "group",
                "grp_object": "FQDN_GROUP"
            },
            {
                "type": "object",
                "net_object": "ds_object_host_v6"
            },
            {
                "type": "object",
                "net_object": "ds_object_network_v4"
            }
        ]
    },
    "net_group2": {
        "name": "net_group2",
        "description": "",
        "members": [
            {
                "net_object": "2001:db8:85a3::",
                "prefix_length": "65"
            },
            {
                "type": "group",
                "grp_object": "ds_group_1"
            },
            {
                "type": "group",
                "grp_object": "net_group1"
            }
        ]
    },
    "net_group3": {
        "name": "net_group3",
        "description": "",
        "members": [
            {
                "type": "object",
                "net_object": "ds_object_range_v6"
            },
            {
                "type": "object",
                "net_object": "hostname"
            },
            {
                "type": "group",
                "grp_object": "net_group2"
            },
            {
                "type": "object",
                "net_object": "tetetet"
            }
        ]
    },
    "net_group4": {
        "name": "net_group4",
        "description": "",
        "members": [
            {
                "type": "172.31.142.0",
                "host": "255.255.255.0"
            },
            {
                "type": "group",
                "grp_object": "net_group3"
            },
            {
                "type": "object",
                "net_object": "net_obj_ipv6"
            }
        ]
    }
}

network_objects = {
    "FQDN_OBJ": {
        "description": "descr",
        "type": "fqdn",
        "fqdn_ip_version": "v4",
        "fqdn_address": "www.example.com"
    },
    "hostname": {
        "type": "subnet",
        "network": "192.168.116.0",
        "netmask": "255.255.255.0"
    },
    "tetetet": {
        "description": "123",
        "type": "host",
        "host": "1.2.3.4"
    },
    "range_test": {
        "description": "range descr",
        "type": "range",
        "start_ip": "1.2.3.4",
        "end_ip": "4.3.2.1"
    },
    "ds_object_host_v4": {
        "description": "some_descr",
        "type": "host",
        "host": "1.2.3.4"
    },
    "ds_object_host_v6": {
        "description": "some descr",
        "type": "host",
        "host": "d46:3dfb:8158:f9fe:132a:d956:f7c8:a69a"
    },
    "ds_object_range_v4": {
        "type": "range",
        "start_ip": "1.2.3.4",
        "end_ip": "5.6.7.8"
    },
    "ds_object_range_v6": {
        "description": "some descr",
        "type": "range",
        "start_ip": "d46:3dfb:8158:f9fe:132a:d956:f7c8:a69a",
        "end_ip": "d46:3dfb:8158:f9fe:132a:d956:f7c8:a69b"
    },
    "ds_object_network_v4": {
        "type": "subnet",
        "network": "1.2.3.0",
        "netmask": "255.255.255.0"
    },
    "ds_object_fqdn_v4": {
        "type": "fqdn",
        "fqdn_ip_version": "v4",
        "fqdn_address": "www.ya.ru"
    },
    "ds_object_fqdn_v6": {
        "description": "some descr",
        "type": "fqdn",
        "fqdn_ip_version": "v6",
        "fqdn_address": "www.example.com"
    },
    "net_obj_ipv6": {
        "type": "subnet",
        "network": "2001:db8:85a3::",
        "prefix_length": "48"
    },
    "172.31.142.54": {
        "type": "host",
        "host": "172.31.142.54"
    },
    "1.1.1.1": {
        "type": "host",
        "host": "1.1.1.1"
    }
}


def parse_member(group_member, parsed_members):
    member_type = group_member.get("type", None)
    if not member_type:
        parsed_members.append(group_member)
        return parsed_members
    if member_type == "object":
        parsed_members.append(network_objects[group_member["net_object"]])
        return parsed_members
    if group_member["type"] == "group":
        group_members = network_groups[group_member["grp_object"]]["members"]
        for in_member in group_members:
            parse_member(in_member, parsed_members)
    else:
        parsed_members.append(group_member)
        return parsed_members


parsed_groups = {}
for group_name, group_inners in network_groups.items():
    tmp_members = []
    for member in group_inners["members"]:
        parse_member(member, tmp_members)
    del group_inners["members"]
    parsed_groups[group_name] = group_inners
    parsed_groups[group_name]["members"] = tmp_members



# GPT Code

# def parse_member(group_member, parsed_members, network_objects, network_groups):
#     member_type = group_member.get("type")
#     if not member_type:
#         parsed_members.append(group_member)
#     elif member_type == "object":
#         parsed_members.append(network_objects.get(group_member["net_object"]))
#     elif member_type == "group":
#         group_members = network_groups.get(group_member["grp_object"], {}).get("members", [])
#         for in_member in group_members:
#             parse_member(in_member, parsed_members, network_objects, network_groups)


# def optimize_groups(network_objects, network_groups):
#     parsed_groups = {}
#     for group_name, group_inners in network_groups.items():
#         tmp_members = []
#         for member in group_inners.get("members", []):
#             parse_member(member, tmp_members, network_objects, network_groups)
#         parsed_groups[group_name] = group_inners.copy()
#         parsed_groups[group_name]["members"] = tmp_members
#         del parsed_groups[group_name]["members"]  # Removing redundant members
#     return parsed_groups


# # Example usage
# optimized_groups = optimize_groups(network_objects, network_groups)