<?php
// generate_bids.php - Generate Random Bids for Products
// File location: /bidwheels.net/dummy/generate_bids.php

require_once '../includes/auth.php';
require_once '../includes/functions.php';
require_once '../admin/includes/admin_header.php';

requireAdmin();

$message = '';
$error = '';

// Get all active products
$products = fetchAll("SELECT id, name, starting_price, current_highest_bid FROM products WHERE is_reserved = 0");
$dummy_users = fetchAll("SELECT id FROM users WHERE is_dummy = 1");

if (count($dummy_users) == 0) {
    $error = 'No dummy users found. Please create dummy users first.';
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $num_bids = isset($_POST['num_bids']) ? (int)$_POST['num_bids'] : 10;
    $product_id = isset($_POST['product_id']) ? (int)$_POST['product_id'] : 0;
    
    if ($product_id > 0) {
        $target_products = fetchAll("SELECT * FROM products WHERE id = $product_id");
    } else {
        $target_products = $products;
    }
    
    $total_bids = 0;
    
    foreach ($target_products as $product) {
        $current_price = $product['current_highest_bid'] ?? $product['starting_price'];
        
        for ($i = 0; $i < $num_bids; $i++) {
            $increment = rand(50, 500);
            $current_price += $increment;
            $random_user = $dummy_users[array_rand($dummy_users)];
            
            insert('bids', [
                'product_id' => $product['id'],
                'user_id' => $random_user['id'],
                'amount' => $current_price,
                'is_system_bid' => 1
            ]);
            $total_bids++;
        }
        
        // 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']}");
        }
    }
    
    $message = "Successfully generated $total_bids bids!";
}

$product_list = fetchAll("SELECT id, name FROM products ORDER BY created_at DESC");
?>

<div class="admin-page">
    <h1 class="page-title">Generate Random Bids</h1>
    
    <?php if ($message): ?>
        <div class="alert alert-success"><?php echo $message; ?></div>
    <?php endif; ?>
    
    <?php if ($error): ?>
        <div class="alert alert-danger"><?php echo $error; ?></div>
    <?php endif; ?>
    
    <div class="info-box" style="background: #e3f2fd; padding: 20px; border-radius: 5px; margin-bottom: 25px;">
        <h3>About This Script</h3>
        <p>This script generates random bids for products using dummy user accounts. Each bid increases by $50-$500.</p>
        <ul>
            <li>Dummy Users Available: <?php echo count($dummy_users); ?></li>
            <li>Active Products: <?php echo count($products); ?></li>
        </ul>
    </div>
    
    <form method="POST" action="" class="admin-form" style="background: white; padding: 20px; border-radius: 5px;">
        <div class="form-row" style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
            <div class="form-group">
                <label>Select Product</label>
                <select name="product_id" class="form-control">
                    <option value="0">All Products</option>
                    <?php foreach ($product_list as $p): ?>
                        <option value="<?php echo $p['id']; ?>"><?php echo htmlspecialchars($p['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="form-group">
                <label>Number of Bids per Product</label>
                <input type="number" name="num_bids" min="1" max="100" value="10" class="form-control" required>
            </div>
        </div>
        <button type="submit" class="btn btn-primary">Generate Bids</button>
    </form>
</div>

<?php require_once '../admin/includes/admin_footer.php'; ?>