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

// --- CONFIG ---
$usersParams = [
    'email' => 'admin@ghadget.com',
    'username' => 'GadgetAdmin',
    'password' => 'Ogaminan2####', // Will be hashed
    'phone' => '07069440262',
    'whatsapp' => '07069440262',
    'verified' => true,
    'avatar' => 'https://ui-avatars.com/api/?name=Gadget+Admin&background=0D8ABC&color=fff',
    'balance' => 500000
];

$dataFile = 'data/listings.json';
$usersFile = 'data/users.json';

// --- ENSURE DIRS ---
if (!is_dir('data')) mkdir('data');

// --- 1. CREATE ADMIN USER ---
$users = [];
if (file_exists($usersFile)) {
    $users = json_decode(file_get_contents($usersFile), true) ?? [];
}

$adminExists = false;
foreach ($users as $u) {
    if ($u['email'] === $usersParams['email']) {
        $adminExists = true;
        break;
    }
}

if (!$adminExists) {
    $newUser = $usersParams;
    $newUser['password'] = password_hash($usersParams['password'], PASSWORD_DEFAULT);
    $newUser['id'] = uniqid();
    $newUser['created_at'] = time();
    $users[] = $newUser;
    file_put_contents($usersFile, json_encode($users, JSON_PRETTY_PRINT));
    echo "Admin User Created.\n";
} else {
    echo "Admin User Already Exists.\n";
}

// --- 2. GENERATE REALISTIC LISTINGS ---
$locations = [
    ['lat' => 6.5244, 'lng' => 3.3792, 'address' => 'Ikeja, Lagos'],
    ['lat' => 6.4698, 'lng' => 3.5852, 'address' => 'Lekki, Lagos'],
    ['lat' => 9.0765, 'lng' => 7.3986, 'address' => 'Wuse II, Abuja'],
    ['lat' => 7.3775, 'lng' => 3.9470, 'address' => 'Bodija, Ibadan']
];

$products = [
    ['title' => 'iPhone 14 Pro Max', 'price' => 950000, 'cat' => 'Smartphones', 'img' => 'https://fdn2.gsmarena.com/vv/pics/apple/apple-iphone-14-pro-max-1.jpg'],
    ['title' => 'Samsung Galaxy S23 Ultra', 'price' => 1100000, 'cat' => 'Smartphones', 'img' => 'https://fdn2.gsmarena.com/vv/pics/samsung/samsung-galaxy-s23-ultra-5g-1.jpg'],
    ['title' => 'MacBook Pro M2 14"', 'price' => 1800000, 'cat' => 'Laptops', 'img' => 'https://fdn2.gsmarena.com/vv/pics/apple/apple-macbook-pro-14-2023-1.jpg'],
    ['title' => 'PlayStation 5 Console', 'price' => 650000, 'cat' => 'Consoles', 'img' => 'https://gmedia.playstation.com/is/image/SIEPDC/ps5-product-thumbnail-01-en-14sep21?$facebook$'],
    ['title' => 'Canon EOS R5 Camera', 'price' => 2500000, 'cat' => 'Cameras', 'img' => 'https://farm4.staticflickr.com/3743/10204646706_0d9509426f_b.jpg'],
    ['title' => 'DJI Mini 3 Pro Drone', 'price' => 750000, 'cat' => 'Drones', 'img' => 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRkQ2kC3_qG8r_L8qD6_w&s'],
    ['title' => 'iPad Pro 12.9 M2', 'price' => 900000, 'cat' => 'Tablets', 'img' => 'https://fdn2.gsmarena.com/vv/pics/apple/apple-ipad-pro-129-2022-1.jpg'],
    ['title' => 'Apple Watch Ultra', 'price' => 600000, 'cat' => 'Smartwatches', 'img' => 'https://fdn2.gsmarena.com/vv/pics/apple/apple-watch-ultra-1.jpg'],
    ['title' => 'Sony WH-1000XM5', 'price' => 350000, 'cat' => 'Audio', 'img' => 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ_k5_L9_w&s'],
    ['title' => 'Xbox Series X', 'price' => 550000, 'cat' => 'Consoles', 'img' => 'https://compass-ssl.xbox.com/assets/f0/85/f085c120-d3d5-4424-8b70-eb25de295865.jpg?n=XBX_A-BuyBoxBGImage04-D.jpg'],
    ['title' => 'Google Pixel 7 Pro', 'price' => 700000, 'cat' => 'Smartphones', 'img' => 'https://fdn2.gsmarena.com/vv/pics/google/google-pixel7-pro-2.jpg'],
    ['title' => 'Dell XPS 15', 'price' => 1400000, 'cat' => 'Laptops', 'img' => 'https://i.dell.com/is/image/DellContent/content/dam/ss2/product-images/dell-client-products/notebooks/xps-notebooks/xps-15-9530/media-gallery/blue/notebook-xps-15-9530-blue-gallery-3.psd?fmt=png-alpha&pscan=auto&scl=1&hei=402&wid=606&qlt=100,1&resMode=sharp2&size=606,402&chrss=full'],
    ['title' => 'GoPro Hero 11 Black', 'price' => 450000, 'cat' => 'Cameras', 'img' => 'https://static.bhphoto.com/images/images500x500/1663162235_1726978.jpg'],
    ['title' => 'Nintendo Switch OLED', 'price' => 380000, 'cat' => 'Consoles', 'img' => 'https://assets.nintendo.com/image/upload/f_auto/q_auto/dpr_2.0/c_scale,w_400/ncom/en_US/switch/site-design-update/hardware/switch/oled-model/gallery/white/01'],
    ['title' => 'HP Spectre x360', 'price' => 1200000, 'cat' => 'Laptops', 'img' => 'https://ssl-product-images.www8-hp.com/digmedialib/prodimg/lowres/c08405908.png'],
    ['title' => 'Samsung Galaxy Tab S9', 'price' => 850000, 'cat' => 'Tablets', 'img' => 'https://fdn2.gsmarena.com/vv/pics/samsung/samsung-galaxy-tab-s9-ultra-5g-1.jpg'],
    ['title' => 'JBL Charge 5 Speaker', 'price' => 150000, 'cat' => 'Audio', 'img' => 'https://www.jbl.com.ng/dw/image/v2/AAuj_PRD/on/demandware.static/-/Sites-masterCatalog_Harman/default/dw12345678/JBL_Charge_5_Hero_Black-1605x1605px.png?sw=537&sfrm=png'],
    ['title' => 'Meta Quest 3', 'price' => 600000, 'cat' => 'Gaming', 'img' => 'https://scontent.flos1-1.fna.fbcdn.net/v/t39.8562-6/358602504_318353344199997_3700021612711718047_n.png?_nc_cat=104&ccb=1-7&_nc_sid=f537c7&_nc_ohc=abcde12345&_nc_ht=scontent.flos1-1.fna&oh=00_AfC&oe=65E12345'],
    ['title' => 'Nikon Z6 II Body', 'price' => 1900000, 'cat' => 'Cameras', 'img' => 'https://cdn-4.nikon-cdn.com/e/Q5nM96RzzObjliYr9kpr9hIdc_Qz7E/Views/1659_Z6II_front.png'],
    ['title' => 'Asus ROG Zephyrus G14', 'price' => 1600000, 'cat' => 'Laptops', 'img' => 'https://dlcdnwebimgs.asus.com/gain/9BB976D8-1234-4567-890A-BCDEF0123456/w1000/h732']
];

$newListings = [];
$time = time();

foreach ($products as $i => $p) {
    $loc = $locations[array_rand($locations)];
    // Randomize price slightly +/- 5%
    $price = $p['price'] * (1 + (rand(-5, 5) / 100));
    $price = round($price, -3); // Round to nearest 1000

    $listing = [
        'id' => $time - ($i * 3600), // Staggered timestamps (1 hour apart)
        'type' => 'sell',
        'title' => $p['title'],
        'price' => $price,
        'category' => $p['cat'],
        'condition' => (rand(0, 1) ? 'New' : 'Used'),
        'description' => "Clean " . $p['title'] . " for sale. Working perfectly. Serious buyers only. Call or WhatsApp me.",
        'location' => $loc,
        'images' => [$p['img']],
        'sellerId' => $usersParams['email'],
        'sellerName' => $usersParams['username'],
        'sellerAvatar' => $usersParams['avatar'],
        'sellerPhone' => $usersParams['phone'],
        'isFeatured' => ($i < 3), // First 3 are featured
        'sellerVerified' => true
    ];
    $newListings[] = $listing;
}

// Merge or Overwrite? Let's overwrite for clean slate as requested ("seed").
file_put_contents($dataFile, json_encode($newListings, JSON_PRETTY_PRINT));

echo "Seeded " . count($newListings) . " Listings successfully.\n";
echo "Done.";
