<?php
// generate_comments.php - Generate Random Comments
// File location: /bidwheels.net/dummy/generate_comments.php

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

requireAdmin();

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

// Comment templates
$comment_templates = [
    "Beautiful vehicle! I'm definitely interested in this one.",
    "Has anyone seen this in person? Would love to know more.",
    "What's the maintenance history like? Any accidents?",
    "This is a steal at this price! Can't believe it.",
    "I've been looking for one of these for months.",
    "Can you share more photos? Especially the interior.",
    "Is the price negotiable or is this final?",
    "Amazing vehicle! Good luck everyone bidding.",
    "I placed my bid! Fingers crossed I win.",
    "This won't last long at this price. Hurry up!",
    "How many miles / hours does it have?",
    "Original owner? Do you have all service records?",
    "Dream vehicle right here! Someone's getting lucky.",
    "I'll be watching this one closely till the end.",
    "What's the reason for selling? Just curious.",
    "Does it come with all original accessories?",
    "Has it been in any accidents or had major repairs?",
    "The color combination is perfect! Love it.",
    "Is financing available or cash only?",
    "Can we arrange a pre-bid inspection?"
];

$products = fetchAll("SELECT id, name 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_comments = isset($_POST['num_comments']) ? (int)$_POST['num_comments'] : 5;
    $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_comments = 0;
    
    foreach ($target_products as $product) {
        for ($i = 0; $i < $num_comments; $i++) {
            $random_user = $dummy_users[array_rand($dummy_users)];
            $random_comment = $comment_templates[array_rand($comment_templates)];
            
            insert('comments', [
                'product_id' => $product['id'],
                'user_id' => $random_user['id'],
                'comment_text' => $random_comment,
                'is_system_comment' => 1
            ]);
            $total_comments++;
        }
    }
    
    $message = "Successfully generated $total_comments comments!";
}

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

<div class="admin-page">
    <h1 class="page-title">Generate Random Comments</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 realistic comments for products using dummy user accounts.</p>
        <ul>
            <li>Dummy Users Available: <?php echo count($dummy_users); ?></li>
            <li>Active Products: <?php echo count($products); ?></li>
            <li>Comment Templates: <?php echo count($comment_templates); ?></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 Comments per Product</label>
                <input type="number" name="num_comments" min="1" max="50" value="5" class="form-control" required>
            </div>
        </div>
        <button type="submit" class="btn btn-primary">Generate Comments</button>
    </form>
</div>

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