<?php
require_once 'config.php';

echo "<h2>Create Test User & Login</h2>";

// Create users table if it doesn't exist
$create_users_table = "CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `usernumber` varchar(15) NOT NULL UNIQUE,
  `password` varchar(255) NOT NULL,
  `reg_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
)";

mysqli_query($link, $create_users_table);

// Check if test user exists
$check_user = mysqli_query($link, "SELECT * FROM users WHERE username = 'testuser'");
if (mysqli_num_rows($check_user) == 0) {
    // Create test user
    $username = 'testuser';
    $usernumber = '1234567890';
    $password = password_hash('test123', PASSWORD_DEFAULT);
    
    $sql = "INSERT INTO users (username, usernumber, password) VALUES (?, ?, ?)";
    $stmt = mysqli_prepare($link, $sql);
    mysqli_stmt_bind_param($stmt, 'sss', $username, $usernumber, $password);
    
    if (mysqli_stmt_execute($stmt)) {
        echo "<p>✅ Test user created successfully!</p>";
        echo "<p><strong>Username:</strong> testuser</p>";
        echo "<p><strong>Number:</strong> 1234567890</p>";
        echo "<p><strong>Password:</strong> test123</p>";
    } else {
        echo "<p>❌ Error creating test user: " . mysqli_error($link) . "</p>";
    }
    mysqli_stmt_close($stmt);
} else {
    echo "<p>✅ Test user already exists!</p>";
    echo "<p><strong>Username:</strong> testuser</p>";
    echo "<p><strong>Number:</strong> 1234567890</p>";
    echo "<p><strong>Password:</strong> test123</p>";
}

// Create entries table and add test entry
$create_entries_table = "CREATE TABLE IF NOT EXISTS `entries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `entry_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `game_type` varchar(50) DEFAULT 'default',
  PRIMARY KEY (`id`)
)";

mysqli_query($link, $create_entries_table);

// Add test entry for today
$today = date('Y-m-d H:i:s');
$check_entry = mysqli_query($link, "SELECT * FROM entries WHERE username = 'testuser' AND DATE(entry_time) = CURDATE()");
if (mysqli_num_rows($check_entry) == 0) {
    mysqli_query($link, "INSERT INTO entries (username, entry_time) VALUES ('testuser', '$today')");
    echo "<p>✅ Test entry added for today</p>";
} else {
    echo "<p>✅ Test entry already exists for today</p>";
}

echo "<hr>";
echo "<h3>Next Steps:</h3>";
echo "<ol>";
echo "<li><a href='login.php' target='_blank'>Go to Login Page</a></li>";
echo "<li>Login with: <strong>1234567890</strong> / <strong>test123</strong></li>";
echo "<li>You should see 'testuser' instead of 'Guest'</li>";
echo "<li>Admin dashboard should show 1 active user</li>";
echo "</ol>";

mysqli_close($link);
?>