<?php
// server/reset_data.php
// PURPOSE: Reset Users and Listings, Seed specific data for testing.
// SECURITY: This should be deleted or protected in production.

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

$usersFile = __DIR__ . '/data/users.json';
$listingsFile = __DIR__ . '/data/listings.json';

// 1. DATA TO KEEP / SEED
$keepEmails = ['apaxure@gmail.com', 'admin@ghadget.com'];

// Define Seed Users
$seedUsers = [
    'admin@ghadget.com' => [
        'id' => 'u_admin',
        'username' => 'GhadgetAdmin',
        'email' => 'admin@ghadget.com',
        'password' => password_hash('Admin123!', PASSWORD_DEFAULT),
        'whatsapp' => '2348123456789', // CHANGE THIS TO REAL ADMIN NUMBER
        'verified' => true,
        'avatar' => 'https://ui-avatars.com/api/?name=Ghadget+Admin&background=000&color=fff'
    ],
    'apaxure@gmail.com' => [
        'id' => 'u_apaxure',
        'username' => 'Apaxure',
        'email' => 'apaxure@gmail.com',
        'password' => password_hash('Pass123!', PASSWORD_DEFAULT),
        'whatsapp' => '2349000000000',
        'verified' => true,
        'avatar' => 'https://ui-avatars.com/api/?name=Apaxure&background=135bec&color=fff'
    ]
];

// 2. PROCESS USERS
$existingUsers = [];
if (file_exists($usersFile)) {
    $existingUsers = json_decode(file_get_contents($usersFile), true) ?: [];
}

// Filter existing or overwrite?
// User said "clear every other users", so we essentially rebuild the list.
// We'll keep the IDs/Hashes if they exist for continuity, or just overwrite with seed.
// To ensure Clean Slate, let's Overwrite with Seed + any specific overrides if needed.
// Actually, let's just save the Seed Users.
$finalUsers = array_values($seedUsers); // Convert back to list if needed, or keyed?
// Auth logic usually expects array of objects.
$finalUsersList = [];
foreach ($seedUsers as $u) {
    $finalUsersList[] = $u;
}

file_put_contents($usersFile, json_encode($finalUsersList, JSON_PRETTY_PRINT));


// 3. SEED LISTINGS (Apaxure Requests)
// User want "products requests" (Type: buy) for Apaxure.
$seedListings = [
    [
        'id' => 'req_001',
        'title' => 'I need a clean iPhone 12 Pro Max',
        'price' => 450000,
        'description' => 'Looking for a UK used iPhone 12 Pro Max, 256GB. Battery health above 90%.',
        'category' => 'Smartphones',
        'type' => 'buy', // Crucial for Buyers Map
        'sellerId' => 'apaxure@gmail.com', // Map to Apaxure
        'sellerName' => 'Apaxure',
        'whatsapp' => '2349000000000',
        'date' => date('c'),
        'location' => ['lat' => 6.5244, 'lng' => 3.3792, 'address' => 'Lagos, Nigeria'] // Lagos Coords
    ],
    [
        'id' => 'req_002',
        'title' => 'Buying PS5 Console (Disc Version)',
        'price' => 600000,
        'description' => 'Urgent. Must be clean with 2 pads.',
        'category' => 'Consoles',
        'type' => 'buy',
        'sellerId' => 'apaxure@gmail.com',
        'sellerName' => 'Apaxure',
        'whatsapp' => '2349000000000',
        'date' => date('c'),
        'location' => ['lat' => 9.0765, 'lng' => 7.3986, 'address' => 'Abuja, Nigeria'] // Abuja Coords
    ]
];

file_put_contents($listingsFile, json_encode($seedListings, JSON_PRETTY_PRINT));

echo json_encode([
    'status' => 'success',
    'message' => 'Clean Slate Executed. Users reset to Admin/Apaxure. Seed listings added.',
    'users' => count($finalUsersList),
    'listings' => count($seedListings)
]);
