<?php
// create_accounts.php - Create Dummy User Accounts
// File location: /bidwheels.net/dummy/create_accounts.php

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

requireAdmin();

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

// Dummy names array
$first_names = [
    'John', 'Emma', 'Michael', 'Sarah', 'James', 'Jessica', 'David', 'Ashley', 
    'Christopher', 'Amanda', 'Daniel', 'Melissa', 'Matthew', 'Laura', 'Kevin', 
    'Rachel', 'Jason', 'Jennifer', 'Brian', 'Nicole', 'Ryan', 'Olivia', 
    'Eric', 'Sophia', 'Andrew', 'Isabella', 'Thomas', 'Mia', 'Charles', 'Charlotte'
];

$last_names = [
    'Smith', 'Johnson', 'Brown', 'Davis', 'Wilson', 'Martinez', 'Anderson', 'Taylor',
    'Thomas', 'Jackson', 'White', 'Harris', 'Martin', 'Thompson', 'Garcia', 'Robinson',
    'Clark', 'Rodriguez', 'Lewis', 'Lee', 'Walker', 'Hall', 'Young', 'King', 'Wright'
];

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $num_users = isset($_POST['num_users']) ? (int)$_POST['num_users'] : 20;
    
    if ($num_users < 1 || $num_users > 100) {
        $error = 'Please enter a number between 1 and 100.';
    } else {
        $created = 0;
        
        for ($i = 0; $i < $num_users; $i++) {
            $first = $first_names[array_rand($first_names)];
            $last = $last_names[array_rand($last_names)];
            $name = $first . ' ' . $last;
            
            // Generate unique email
            $domain = $domains[array_rand($domains)];
            $email = strtolower($first . '.' . $last . rand(1, 999)) . '@' . $domain;
            
            $phone = rand(100, 999) . '-' . rand(100, 999) . '-' . rand(1000, 9999);
            
            // Check if email exists
            $existing = fetchOne("SELECT id FROM users WHERE email = '$email'");
            if (!$existing) {
                insert('users', [
                    'name' => $name,
                    'email' => $email,
                    'password' => md5(DUMMY_PASSWORD),
                    'phone' => $phone,
                    'role' => 'user',
                    'is_dummy' => 1
                ]);
                $created++;
            }
        }
        
        $message = "Successfully created $created new dummy user accounts!";
        
        // Log the dummy password for admin reference
        $_SESSION['dummy_password_shown'] = DUMMY_PASSWORD;
    }
}
?>

<div class="admin-page">
    <h1 class="page-title">Create Dummy User Accounts</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; ?>
    
    <?php if (isset($_SESSION['dummy_password_shown'])): ?>
        <div class="alert alert-info">
            <strong>Important:</strong> All dummy accounts have the fixed password: <code><?php echo $_SESSION['dummy_password_shown']; ?></code>
        </div>
    <?php endif; ?>
    
    <div class="info-box">
        <h3><i class="fas fa-info-circle"></i> About Dummy Accounts</h3>
        <p>Dummy accounts are used to simulate real user activity on the platform. They can:</p>
        <ul>
            <li>Place automated bids on products</li>
            <li>Post realistic comments</li>
            <li>Make the platform appear active and engaging</li>
        </ul>
        <p><strong>Note:</strong> Dummy accounts cannot log in to the frontend. They are only used for system-generated activity.</p>
        <p><strong>Fixed Password:</strong> <code><?php echo DUMMY_PASSWORD; ?></code></p>
    </div>
    
    <div class="stats-box">
        <div class="stat">
            <span class="stat-label">Existing Dummy Users</span>
            <span class="stat-value"><?php echo getCount('users', 'is_dummy = 1'); ?></span>
        </div>
        <div class="stat">
            <span class="stat-label">Existing Real Users</span>
            <span class="stat-value"><?php echo getCount('users', 'is_dummy = 0 AND role = "user"'); ?></span>
        </div>
    </div>
    
    <form method="POST" action="" class="admin-form">
        <div class="form-group">
            <label>Number of Dummy Users to Create</label>
            <input type="number" name="num_users" min="1" max="100" value="20" class="form-control" required>
            <small>Maximum 100 users at a time</small>
        </div>
        
        <button type="submit" class="btn btn-primary"><i class="fas fa-user-plus"></i> Create Dummy Users</button>
        <a href="../admin/users.php" class="btn btn-secondary">View All Users</a>
    </form>
    
    <div class="existing-users">
        <h3>Recent Dummy Users</h3>
        <?php
        $recent_dummies = fetchAll("SELECT * FROM users WHERE is_dummy = 1 ORDER BY id DESC LIMIT 10");
        ?>
        <?php if (count($recent_dummies) > 0): ?>
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Phone</th>
                        <th>Created</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($recent_dummies as $dummy): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($dummy['name']); ?></td>
                            <td><?php echo htmlspecialchars($dummy['email']); ?></td>
                            <td><?php echo htmlspecialchars($dummy['phone']); ?></td>
                            <td><?php echo date('M d, Y', strtotime($dummy['created_at'])); ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?>
            <p>No dummy users created yet.</p>
        <?php endif; ?>
    </div>
</div>

<style>
.info-box {
    background: #e3f2fd;
    border-left: 4px solid #2196f3;
    padding: 20px;
    border-radius: 5px;
    margin-bottom: 25px;
}
.info-box ul {
    margin-top: 10px;
    margin-left: 20px;
}
.stats-box {
    background: white;
    padding: 20px;
    border-radius: 5px;
    margin-bottom: 25px;
    display: flex;
    justify-content: center;
    gap: 50px;
}
.stat {
    text-align: center;
}
.stat-label {
    display: block;
    font-size: 14px;
    color: #666;
}
.stat-value {
    display: block;
    font-size: 36px;
    font-weight: bold;
    color: #e63946;
}
.existing-users {
    margin-top: 30px;
    background: white;
    padding: 20px;
    border-radius: 5px;
}
.existing-users h3 {
    margin-bottom: 20px;
}
</style>

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