<?php
// server/create_mock_user.php

$hasher = function ($pass) {
    return password_hash($pass, PASSWORD_DEFAULT);
};

$file = __DIR__ . '/data/users.json';
$users = [];

if (file_exists($file)) {
    $users = json_decode(file_get_contents($file), true);
    if (!is_array($users)) $users = [];
}

$mockUser = [
    'id' => 'user_' . time(),
    'username' => 'Demo User',
    'email' => 'demo@ghadget.com',
    'password' => $hasher('password123'),
    'whatsapp' => '2348000000000',
    'balance' => 50000,
    'avatar' => 'https://ui-avatars.com/api/?name=Demo+User&background=0D8ABC&color=fff',
    'verified' => true,
    'created_at' => date('Y-m-d H:i:s')
];

// Check if exists
$exists = false;
foreach ($users as $key => $u) {
    if ($u['email'] === $mockUser['email']) {
        $users[$key] = $mockUser; // Update
        $exists = true;
        break;
    }
}

if (!$exists) {
    $users[] = $mockUser;
}

file_put_contents($file, json_encode($users, JSON_PRETTY_PRINT));

echo "Mock User Created:\n";
echo "Email: " . $mockUser['email'] . "\n";
echo "Password: password123\n";
