<?php

// Function to generate a UUID (compliant with MySQL UUID)
function generateUUID() {
    return bin2hex(random_bytes(16));  // Generate a random UUID
}

// Function to convert the raw SVG to JSON format
function convertSymbolsToJSON($svgContent) {
    // Match all <symbol> elements and extract them
    preg_match_all('/<symbol[^>]*>[\s\S]*?<\/symbol>/', $svgContent, $matches);
    
    $jsonData = [];

    foreach ($matches[0] as $symbol) {
        // Extract 'id', 'viewBox', and 'path' attributes
        preg_match('/id="([^"]+)"/', $symbol, $idMatches);
        preg_match('/viewBox="([^"]+)"/', $symbol, $viewBoxMatches);
        preg_match('/<path[^>]*d="([^"]+)"/', $symbol, $pathMatches);

        // If we have a valid symbol, process it
        if (isset($idMatches[1]) && isset($viewBoxMatches[1]) && isset($pathMatches[1])) {
            // Generate a UUID for the symbol
            $uniqueId = generateUUID();

            // Capitalize the name by replacing hyphens with spaces and capitalizing each word
            $name = ucwords(str_replace('-', ' ', $idMatches[1]));

            // Build the symbol JSON object
            $symbolJSON = [
                "id" => $uniqueId,
                "name" => $name,
                "viewBox" => $viewBoxMatches[1],
                "path" => "<path d='" . $pathMatches[1] . "'/>"
            ];

            // Add the symbol JSON to the data array
            $jsonData[] = $symbolJSON;
        }
    }

    return $jsonData;
}

// Read the SVG file (assumes it's in the same directory)
$svgFilePath = 'input.svg'; // The input SVG file
if (file_exists($svgFilePath)) {
    $svgContent = file_get_contents($svgFilePath);

    // Convert symbols to JSON
    $symbolsJson = convertSymbolsToJSON($svgContent);

    // Output the JSON data to a file
    $outputFilePath = 'output.json';
    file_put_contents($outputFilePath, json_encode($symbolsJson, JSON_PRETTY_PRINT));

    echo "JSON file has been created successfully: $outputFilePath\n";
} else {
    echo "Error: SVG file not found.\n";
}
?>
