# import xml.etree.ElementTree as ET

# tree = ET.parse("floor-plan-coloring.svg")
# root = tree.getroot()

# apartments = []

# for elem in root.iter():
#     tag = elem.tag.split("}")[-1]  # remove namespace

#     if tag in ["polygon", "polyline"]:
#         unit = elem.attrib.get("id")
#         points = elem.attrib.get("points")

#         if not points:
#             continue

#         coords = []
#         for p in points.split():
#             x, y = p.split(",")
#             coords.append({
#                 "x": float(x),
#                 "y": float(y)
#             })

#         apartments.append({
#             "unit": unit,
#             "coordinates": coords
#         })

#     elif tag == "path":
#         unit = elem.attrib.get("id")
#         d = elem.attrib.get("d")

#         apartments.append({
#             "unit": unit,
#             "path_data": d
#         })

# print(apartments)

# import xml.etree.ElementTree as ET

# input_file = "floor-plan-coloring.svg"
# output_file = "output.svg"

# tree = ET.parse(input_file)
# root = tree.getroot()

# apartments = []
# count = 1

# for elem in root.iter():
#     tag = elem.tag.split("}")[-1]  # remove namespace

#     if tag in ["polygon", "polyline"]:
#         unit = elem.attrib.get("id")
#         points = elem.attrib.get("points")

#         if not points:
#             continue

#         coords = []
#         for p in points.split():
#             x, y = p.split(",")
#             coords.append({
#                 "x": float(x),
#                 "y": float(y)
#             })

#         apartments.append({
#             "unit": unit,
#             "coordinates": coords
#         })

#         # 🔴 Highlight shape
#         elem.set("stroke", "red")
#         elem.set("stroke-width", "3")
#         elem.set("fill", "yellow")
#         elem.set("fill-opacity", "0.4")

#     elif tag == "path":
#         unit = elem.attrib.get("id")
#         d = elem.attrib.get("d")

#         apartments.append({
#             "unit": unit,
#             "path_data": d
#         })

#         # 🔴 Highlight path
#         elem.set("stroke", "red")
#         elem.set("stroke-width", "3")
#         elem.set("fill", "yellow")
#         elem.set("fill-opacity", "0.4")

#         # Optional: auto-assign ID if missing
#         if not unit:
#             elem.set("id", f"unit_{count}")
#             count += 1

# # Save new SVG file
# tree.write(output_file)

# print("Apartments detected:", len(apartments))
# print("New SVG created:", output_file)

# import xml.etree.ElementTree as ET

# input_file = "floor-plan-coloring.svg"
# output_file = "output.svg"

# ET.register_namespace("", "http://www.w3.org/2000/svg")

# tree = ET.parse(input_file)
# root = tree.getroot()

# count = 1

# for elem in root.iter():
#     tag = elem.tag.split("}")[-1]

#     if tag in ["path", "polygon", "polyline"]:

#         # 🔥 REMOVE existing style attribute (important)
#         if "style" in elem.attrib:
#             del elem.attrib["style"]

#         # 🔴 Force highlight
#         elem.set("fill", "yellow")
#         elem.set("stroke", "red")
#         elem.set("stroke-width", "4")
#         elem.set("opacity", "0.6")

#         # Optional: assign ID if missing
#         if not elem.attrib.get("id"):
#             elem.set("id", f"unit_{count}")
#             count += 1

# tree.write(output_file)

# print("SVG highlighted and saved as:", output_file)

# import re
# import copy
# import xml.etree.ElementTree as ET

# def extract_coords_from_path(d):
#     numbers = re.findall(r"[-+]?\d*\.\d+|\d+", d)
#     numbers = list(map(float, numbers))
#     coords = []
    
#     for i in range(0, len(numbers), 2):
#         if i + 1 < len(numbers):
#             coords.append({
#                 "x": numbers[i],
#                 "y": numbers[i+1]
#             })
    
#     return coords


# input_file = "floor-plan-coloring.svg"
# output_file = "output.svg"

# ET.register_namespace("", "http://www.w3.org/2000/svg")

# tree = ET.parse(input_file)
# root = tree.getroot()

# ns = {"svg": "http://www.w3.org/2000/svg"}

# apartments = []
# count = 1

# for clip in root.findall(".//svg:clipPath", ns):
#     for elem in clip:
#         tag = elem.tag.split("}")[-1]

#         if tag == "path":
#             d = elem.attrib.get("d")

#             coords = extract_coords_from_path(d)

#             apartments.append({
#                 "unit": f"unit_{count}",
#                 "coordinates": coords
#             })

#             # Overlay highlight
#             new_path = copy.deepcopy(elem)
#             new_path.set("fill", "yellow")
#             new_path.set("stroke", "red")
#             new_path.set("stroke-width", "3")
#             new_path.set("opacity", "0.6")
#             new_path.set("id", f"unit_{count}")

#             root.append(new_path)
#             count += 1

# tree.write(output_file)

# print("Apartments with coordinates:")
# print(apartments)

import copy
import xml.etree.ElementTree as ET
from svg.path import parse_path

def extract_absolute_points_from_path(d):
    path = parse_path(d)
    coords = []

    for segment in path:
        # Get starting point of each segment
        start = segment.start
        coords.append({
            "x": float(start.real),
            "y": float(start.imag)
        })

    # Add final endpoint
    if len(path) > 0:
        end = path[-1].end
        coords.append({
            "x": float(end.real),
            "y": float(end.imag)
        })

    return coords


input_file = "floor-plan-coloring.svg"
output_file = "output.svg"

ET.register_namespace("", "http://www.w3.org/2000/svg")

tree = ET.parse(input_file)
root = tree.getroot()

ns = {"svg": "http://www.w3.org/2000/svg"}

apartments = []
count = 1

for clip in root.findall(".//svg:clipPath", ns):
    for elem in clip:
        tag = elem.tag.split("}")[-1]

        if tag == "path":
            d = elem.attrib.get("d")

            coords = extract_absolute_points_from_path(d)

            apartments.append({
                "unit": f"unit_{count}",
                "coordinates": coords,
                "svg_path": d   # also store original path (important!)
            })

            # Overlay highlight
            new_path = copy.deepcopy(elem)

            # Remove clip-path reference if exists
            if "clip-path" in new_path.attrib:
                del new_path.attrib["clip-path"]

            # Apply visible styles
            new_path.set("fill", "yellow")
            new_path.set("stroke", "red")
            new_path.set("stroke-width", "2")
            new_path.set("opacity", "0.6")
            new_path.set("id", f"unit_{count}")

            # Optional: make sure it's visible
            new_path.set("pointer-events", "all")

            root.append(new_path)
            count += 1
tree.write(output_file)

print("Apartments with coordinates:")
print(apartments)