<?php

// Function to capitalize the 'name' field (capitalizes acronyms fully)
function capitalizeName($name) {
    // Define a pattern to match acronyms (words in uppercase with no spaces)
    $acronymPattern = '/\b[A-Z]{2,}\b/';

    // Capitalize each word, and keep acronyms fully uppercase
    $name = preg_replace_callback('/\b\w+\b/', function ($matches) use ($acronymPattern) {
        $word = $matches[0];
        // If it's an acronym (all uppercase), return as is
        if (preg_match($acronymPattern, $word)) {
            return strtoupper($word);
        }
        // Otherwise capitalize the first letter of each word and lowercase the rest
        return ucfirst(strtolower($word));
    }, $name);

    return $name;
}

// Function to generate a MySQL UUID format (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
function generateMysqlUuid() {
    // Generate a UUID using PHP's random_bytes for a total of 16 random bytes
    $data = random_bytes(16);
    
    // Set the version to 4 (random UUID)
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40);  // Version 4
    // Set the variant to RFC4122
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80);  // RFC4122 variant

    // Now unpack the data into 5 parts for formatting as UUID
    $hexData = unpack('H8a/H4b/H4c/H4d/H12e', $data);

    // Return the UUID in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
    return $hexData['a'] . '-' . $hexData['b'] . '-' . $hexData['c'] . '-' . $hexData['d'] . '-' . $hexData['e'];
}

// Get the current directory of the script
$currentDir = __DIR__;

// Get all JSON files in the current directory
$jsonFiles = glob($currentDir . '/*.json');

// Iterate over each JSON file
foreach ($jsonFiles as $file) {
    // Read the content of the file
    $jsonContent = file_get_contents($file);

    // Decode JSON to array
    $data = json_decode($jsonContent, true);

    // Check if the data is valid
    if (json_last_error() === JSON_ERROR_NONE) {
        // Process each SVG object
        foreach ($data as &$set) {
            if (isset($set['svgs'])) {
                foreach ($set['svgs'] as &$svg) {
                    // Capitalize the 'name' field
                    if (isset($svg['name'])) {
                        $svg['name'] = capitalizeName($svg['name']);
                    }

                    // Generate a new MySQL UUID for the 'id' field
                    if (isset($svg['id'])) {
                        $svg['id'] = generateMysqlUuid();
                    }
                }
            }
        }

        // Re-encode the modified data to JSON
        $newJsonContent = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

        // Define the output file name (you can change this as needed)
        $outputFile = $currentDir . '/modified_' . basename($file);

        // Save the updated content back to a new file
        file_put_contents($outputFile, $newJsonContent);

        echo "Processed and saved to: " . basename($outputFile) . "\n";
    } else {
        echo "Invalid JSON in file: " . basename($file) . "\n";
    }
}
?>
