<?php

// Path to the input JSON file
$inputFile = 'material-icons-baseline.json';
// Path to the output JSON file
$outputFile = 'material-icons-baseline-unescaped-with-ids.json';

// Function to generate a MySQL-style UUID
function generateUUID() {
    // Generates a version 4 UUID (random)
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), 
        mt_rand(0, 0xffff), 
        mt_rand(0, 0x0fff) | 0x4000, 
        mt_rand(0, 0x3fff) | 0x8000, 
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
}

// Step 1: Load the JSON data
$jsonData = file_get_contents($inputFile);

// Check if the file was read successfully
if ($jsonData === false) {
    die("Error reading the JSON file.");
}

// Step 2: Decode the JSON data into a PHP array
$data = json_decode($jsonData, true);

// Check if JSON decoding was successful
if ($data === null) {
    die("Error decoding the JSON data.");
}

// Step 3: Iterate through each item and unescape the 'paths' key, and generate a new 'id'
foreach ($data as &$icon) {
    // Unescape only HTML entities (without affecting forward slashes)
    $icon['paths'] = html_entity_decode($icon['paths'], ENT_QUOTES | ENT_HTML5);
    
    // Generate a new MySQL-style UUID for each 'id'
    $icon['id'] = generateUUID();
}

// Step 4: Encode the modified data back into JSON format
$newJsonData = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);

// Check if encoding was successful
if ($newJsonData === false) {
    die("Error encoding the JSON data.");
}

// Step 5: Save the modified JSON data to the output file
file_put_contents($outputFile, $newJsonData);

// Check if file writing was successful
if (file_put_contents($outputFile, $newJsonData) === false) {
    die("Error saving the modified JSON data.");
}

echo "Paths have been unescaped, IDs have been added, and saved to '$outputFile'.\n";

?>
