<?php
// Add default color codes to existing products
$file = 'data/products.json';
$products = json_decode(file_get_contents($file), true);

$colorMap = [
    'red' => '#FF0000',
    'blue' => '#0000FF',
    'pink' => '#FF69B4',
    'green' => '#008000',
    'white' => '#FFFFFF',
    'black' => '#000000',
    'yellow' => '#FFFF00',
    'purple' => '#800080',
    'brown' => '#8B4513',
    'orange' => '#FFA500'
];

foreach ($products as &$product) {
    if (isset($product['colors']) && is_array($product['colors'])) {
        foreach ($product['colors'] as &$color) {
            if (!isset($color['code']) || empty($color['code'])) {
                $colorName = strtolower($color['name']);
                $color['code'] = $colorMap[$colorName] ?? '#CCCCCC';
            }
        }
    }
}

file_put_contents($file, json_encode($products, JSON_PRETTY_PRINT));
echo 'Color codes added successfully! Redirecting...';
header('Location: index.php');
exit;
?>
