<?php
// dummy_functions.php - Dummy Data Helper Functions
// File location: /bidwheels.net/includes/dummy_functions.php

<?php
// dummy_functions.php - Dummy Data Helper Functions
// File location: /bidwheels.net/includes/dummy_functions.php

require_once __DIR__ . '/db.php';
require_once __DIR__ . '/functions.php';

// Dummy names arrays
$DUMMY_FIRST_NAMES = [
    'John', 'Emma', 'Michael', 'Sarah', 'James', 'Jessica', 'David', 'Ashley',
    'Christopher', 'Amanda', 'Daniel', 'Melissa', 'Matthew', 'Laura', 'Kevin',
    'Rachel', 'Jason', 'Jennifer', 'Brian', 'Nicole', 'Ryan', 'Olivia'
];

$DUMMY_LAST_NAMES = [
    'Smith', 'Johnson', 'Brown', 'Davis', 'Wilson', 'Martinez', 'Anderson', 'Taylor',
    'Thomas', 'Jackson', 'White', 'Harris', 'Martin', 'Thompson', 'Garcia', 'Robinson'
];

$DUMMY_DOMAINS = ['gmail.com', 'yahoo.com', 'hotmail.com', 'outlook.com', 'aol.com'];

// Comment templates
$COMMENT_TEMPLATES = [
    "Beautiful vehicle! I'm definitely interested.",
    "Has anyone seen this in person?",
    "What's the maintenance history like?",
    "This is a steal at this price!",
    "I've been looking for one of these.",
    "Can you share more photos?",
    "Is the price negotiable?",
    "Amazing vehicle! Good luck everyone.",
    "I placed my bid! Fingers crossed.",
    "This won't last long at this price.",
    "Dream vehicle right here!",
    "Someone's getting a great deal.",
    "I'll be watching this one closely."
];

/**
 * Generate dummy user account
 */
function generateDummyUser($password = null) {
    global $DUMMY_FIRST_NAMES, $DUMMY_LAST_NAMES, $DUMMY_DOMAINS;
    
    $first = $DUMMY_FIRST_NAMES[array_rand($DUMMY_FIRST_NAMES)];
    $last = $DUMMY_LAST_NAMES[array_rand($DUMMY_LAST_NAMES)];
    $name = $first . ' ' . $last;
    
    $domain = $DUMMY_DOMAINS[array_rand($DUMMY_DOMAINS)];
    $email = strtolower($first . '.' . $last . rand(1, 999)) . '@' . $domain;
    
    $phone = rand(100, 999) . '-' . rand(100, 999) . '-' . rand(1000, 9999);
    
    $user_id = insert('users', [
        'name' => $name,
        'email' => $email,
        'password' => md5($password ?? DUMMY_PASSWORD),
        'phone' => $phone,
        'role' => 'user',
        'is_dummy' => 1
    ]);
    
    return $user_id;
}

/**
 * Generate multiple dummy users
 */
function generateDummyUsers($count, $password = null) {
    $created = 0;
    for ($i = 0; $i < $count; $i++) {
        $user_id = generateDummyUser($password);
        if ($user_id) $created++;
    }
    return $created;
}

/**
 * Generate random bids for a product
 */
function generateRandomBids($product_id, $num_bids, $dummy_users = null) {
    if (!$dummy_users) {
        $dummy_users = fetchAll("SELECT id FROM users WHERE is_dummy = 1");
    }
    
    if (count($dummy_users) == 0) {
        return 0;
    }
    
    $product = fetchOne("SELECT starting_price, current_highest_bid FROM products WHERE id = $product_id");
    $current_price = $product['current_highest_bid'] ?? $product['starting_price'];
    
    $bids_placed = 0;
    for ($i = 0; $i < $num_bids; $i++) {
        $increment = rand(50, 500);
        $current_price += $increment;
        $user = $dummy_users[array_rand($dummy_users)];
        
        insert('bids', [
            'product_id' => $product_id,
            'user_id' => $user['id'],
            'amount' => $current_price,
            'is_system_bid' => 1
        ]);
        $bids_placed++;
    }
    
    // Update current highest bid
    $highest = fetchOne("SELECT MAX(amount) as max FROM bids WHERE product_id = $product_id");
    if ($highest['max']) {
        update('products', ['current_highest_bid' => $highest['max']], "id = $product_id");
    }
    
    return $bids_placed;
}

/**
 * Generate random comments for a product
 */
function generateRandomComments($product_id, $num_comments, $dummy_users = null) {
    global $COMMENT_TEMPLATES;
    
    if (!$dummy_users) {
        $dummy_users = fetchAll("SELECT id FROM users WHERE is_dummy = 1");
    }
    
    if (count($dummy_users) == 0) {
        return 0;
    }
    
    $comments_placed = 0;
    for ($i = 0; $i < $num_comments; $i++) {
        $user = $dummy_users[array_rand($dummy_users)];
        $comment = $COMMENT_TEMPLATES[array_rand($COMMENT_TEMPLATES)];
        
        insert('comments', [
            'product_id' => $product_id,
            'user_id' => $user['id'],
            'comment_text' => $comment,
            'is_system_comment' => 1
        ]);
        $comments_placed++;
    }
    
    return $comments_placed;
}

/**
 * Generate complete dummy activity for a new product
 */
function generateProductDummyActivity($product_id) {
    $dummy_users = fetchAll("SELECT id FROM users WHERE is_dummy = 1 LIMIT 30");
    
    if (count($dummy_users) == 0) {
        return ['bids' => 0, 'comments' => 0];
    }
    
    $num_bids = rand(15, 25);
    $num_comments = rand(15, 25);
    
    $bids = generateRandomBids($product_id, $num_bids, $dummy_users);
    $comments = generateRandomComments($product_id, $num_comments, $dummy_users);
    
    return ['bids' => $bids, 'comments' => $comments];
}
?>