<?php
header('Content-Type: application/json');

// Define paths
$productsFile = '../js/products.json';
$backupDir = '../js/backups/';

// Create backup directory if not exists
if (!is_dir($backupDir)) {
    mkdir($backupDir, 0755, true);
}

// Read current products
$currentProducts = file_exists($productsFile) ? 
    json_decode(file_get_contents($productsFile), true) : [];

// Create backup
if (!empty($currentProducts)) {
    $backupFile = $backupDir . 'products_backup_' . date('Ymd_His') . '.json';
    file_put_contents($backupFile, json_encode($currentProducts, JSON_PRETTY_PRINT));
}

// Demo product data
$demoProduct = [
    [
        "id" => "demo001",
        "name" => "Demo Product",
        "color" => "Default",
        "size" => "Free Size",
        "storage" => "",
        "selling_price" => "395",
        "mrp" => "3249",
        "fetaures" => "<p>This is a demo product after reset</p>",
        "img1" => "https://via.placeholder.com/600x400?text=Demo+Product",
        "img2" => "https://via.placeholder.com/600x400?text=Product+Image+2",
        "img3" => "https://via.placeholder.com/600x400?text=Product+Image+3",
        "img4" => "https://via.placeholder.com/600x400?text=Product+Image+4",
        "img5" => "https://via.placeholder.com/600x400?text=Product+Image+5",
        "disp_order" => "0",
        "from_csv" => "0",
        "created_at" => date('Y-m-d H:i:s'),
        "md5_id" => md5(uniqid()),
        "verients" => [
            [
                "id" => "demo001",
                "product_id" => "demo001",
                "name" => "Demo Product",
                "color" => "Default",
                "size" => "Free Size",
                "storage" => "",
                "selling_price" => "395",
                "mrp" => "3199",
                "fetaures" => "<p>This is a demo variant</p>",
                "img1" => "https://via.placeholder.com/600x400?text=Demo+Variant",
                "img2" => "",
                "img3" => "",
                "img4" => "",
                "img5" => "",
                "from_csv" => "0",
                "created_at" => date('Y-m-d H:i:s')
            ]
        ]
    ]
];

// Save demo product
file_put_contents($productsFile, json_encode($demoProduct, JSON_PRETTY_PRINT));

// Response
echo json_encode([
    'success' => true,
    'message' => 'Products reset successfully! Backup created: ' . ($backupFile ?? 'No products to backup')
]);
?>