import sys
import xml.etree.ElementTree as ET
from pathlib import Path

def check_unique_prefix(file_path, unique_prefix):
    """Check if SVG elements already have the unique prefix"""
    try:
        # Parse SVG efficiently
        tree = ET.parse(file_path)
        root = tree.getroot()

        # Define SVG namespace
        namespaces = {'svg': 'http://www.w3.org/2000/svg'}

        # Check all elements with id attributes
        for elem in root.iter():
            elem_id = elem.get('id')
            if elem_id and elem_id.startswith(unique_prefix):
                return True  # Already has prefix

        # Check class attributes too
        for elem in root.iter():
            elem_class = elem.get('class')
            if elem_class and unique_prefix in elem_class:
                return True

        return False  # Needs processing

    except Exception as e:
        print(f"Error: {e}", file=sys.stderr)
        return False

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python check_svg_unique.py <file_path> <unique_prefix>")
        sys.exit(1)

    file_path = sys.argv[1]
    unique_prefix = sys.argv[2]

    has_prefix = check_unique_prefix(file_path, unique_prefix)

    # Return 0 if already has prefix, 1 if needs processing
    sys.exit(0 if has_prefix else 1)
