<?php
// Standalone script to bulk upload products from pics_ecom folder
// Run this once to upload all images as products

require_once __DIR__ . '/config/image_config.php';

$productsFile = __DIR__ . '/data/products.json';
$products = [];
if (file_exists($productsFile)) {
    $products = json_decode(file_get_contents($productsFile), true) ?: [];
}

$picsDir = __DIR__ . '/pics_ecom';
$imageFiles = [];

// Get all image files
if (is_dir($picsDir)) {
    $files = scandir($picsDir);
    foreach ($files as $file) {
        if ($file === '.' || $file === '..' || $file === 'logo.png' || $file === 'placeholder.png') continue;
        
        $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
        if (in_array($ext, ['jpg', 'jpeg', 'png', 'jfif', 'gif', 'webp'])) {
            $imageFiles[] = $file;
        }
    }
}

// Product titles and descriptions
$productTitles = [
    'Elegant Summer Dress',
    'Classic White Shirt',
    'Designer Handbag',
    'Stylish Sunglasses',
    'Casual Denim Jacket',
    'Formal Black Suit',
    'Trendy Sneakers',
    'Leather Watch',
    'Silk Scarf',
    'Wool Coat',
    'Cotton T-Shirt',
    'Running Shoes',
    'Evening Gown',
    'Sports Jacket',
    'Designer Perfume',
    'Fashionable Hat',
    'Leather Boots',
    'Casual Shorts',
    'Formal Pants',
    'Designer Jewelry',
    'Stylish Belt',
    'Winter Gloves',
    'Summer Sandals',
    'Evening Clutch',
    'Designer Wallet',
    'Fashion Scarf',
    'Casual Cap',
    'Formal Shoes',
    'Designer Bag',
    'Stylish Accessories',
    'Vintage Jacket',
    'Modern Blazer',
    'Casual Jeans',
    'Designer Dress',
    'Fashion Boots'
];

$categories = ['Dresses', 'Tops', 'Outerwear', 'Accessories', 'Shoes', 'Bags', 'Jewelry'];
$tagsList = ['fashion', 'trendy', 'stylish', 'elegant', 'casual', 'formal', 'designer', 'luxury', 'summer', 'winter', 'new arrivals', 'bestseller'];
$brandsList = ['Fashion Brand', 'Style Co', 'Trendy Wear', 'Classic Collection', 'Modern Style'];

$uploaded = 0;
$skipped = 0;

foreach ($imageFiles as $idx => $file) {
    // Check if product already exists with this image
    $imagePath = 'pics_ecom/' . $file;
    $exists = false;
    foreach ($products as $p) {
        if (isset($p['image']) && $p['image'] === $imagePath) {
            $exists = true;
            break;
        }
    }
    
    if ($exists) {
        $skipped++;
        continue;
    }
    
    // Get next ID
    $maxId = 0;
    foreach ($products as $p) {
        if (isset($p['id']) && $p['id'] > $maxId) {
            $maxId = $p['id'];
        }
    }
    $newId = $maxId + 1;
    
    // Generate product data
    $title = $productTitles[$idx % count($productTitles)] . ' ' . ($idx + 1);
    $price = rand(19, 999);
    $category = $categories[$idx % count($categories)];
    $tags = implode(', ', array_slice($tagsList, rand(0, count($tagsList) - 3), 3));
    $brand = $brandsList[$idx % count($brandsList)];
    $rating = round((rand(30, 50) / 10), 1); // 3.0 to 5.0
    $desc = "Beautiful and stylish {$title}. Perfect for any occasion. High quality materials and excellent craftsmanship.";
    
    // Randomly add sale price
    $salePrice = null;
    $onSale = false;
    if (rand(1, 3) === 1) { // 33% chance of being on sale
        $salePrice = round($price * 0.7, 2); // 30% off
        $onSale = true;
    }
    
    // Random sizes
    $sizeOptions = ['XS', 'S', 'M', 'L', 'XL', 'XXL'];
    $numSizes = rand(3, 6);
    $sizes = array_slice($sizeOptions, 0, $numSizes);
    
    // Random colors
    $colorOptions = [
        ['name' => 'red', 'code' => '#FF0000'],
        ['name' => 'blue', 'code' => '#0000FF'],
        ['name' => 'green', 'code' => '#008000'],
        ['name' => 'black', 'code' => '#000000'],
        ['name' => 'white', 'code' => '#FFFFFF'],
        ['name' => 'pink', 'code' => '#FF69B4'],
        ['name' => 'purple', 'code' => '#800080'],
        ['name' => 'yellow', 'code' => '#FFFF00'],
        ['name' => 'orange', 'code' => '#FFA500'],
        ['name' => 'brown', 'code' => '#8B4513']
    ];
    $numColors = rand(2, 4);
    $colors = array_slice($colorOptions, 0, $numColors);
    
    $newProduct = [
        'id' => $newId,
        'title' => $title,
        'price' => $price,
        'cat' => $category,
        'desc' => $desc,
        'tags' => $tags,
        'image' => $imagePath,
        'gallery' => [],
        'brand' => $brand,
        'rating' => $rating,
        'sizes' => $sizes,
        'colors' => $colors
    ];
    
    if ($salePrice !== null) {
        $newProduct['sale_price'] = $salePrice;
        $newProduct['on_sale'] = true;
    }
    
    // Randomly mark some as featured
    if (rand(1, 3) === 1) {
        $newProduct['featured'] = true;
    }
    
    $products[] = $newProduct;
    $uploaded++;
}

// Save products
file_put_contents($productsFile, json_encode($products, JSON_PRETTY_PRINT));

echo "<!DOCTYPE html><html><head><title>Bulk Upload Complete</title></head><body>";
echo "<h1>Bulk Upload Complete</h1>";
echo "<p>Uploaded: {$uploaded} products</p>";
echo "<p>Skipped: {$skipped} products (already exist)</p>";
echo "<p>Total products: " . count($products) . "</p>";
echo "<p><a href='shop.php'>Go to Shop</a> | <a href='admin_products.php'>Admin Products</a></p>";
echo "</body></html>";
?>
