<?php
$conn = new mysqli("localhost", "root", "", "paycodex_codex");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "<h3>Step 1: Dropping corrupted table...</h3>";
$drop = $conn->query("DROP TABLE IF EXISTS users");
if ($drop) {
    echo "✓ Table dropped successfully<br><br>";
} else {
    echo "✗ Error dropping table: " . $conn->error . "<br><br>";
}

echo "<h3>Step 2: Recreating users table...</h3>";

// Recreate the users table with common fields
$create = "CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mobile` varchar(15) NOT NULL,
  `email` varchar(100) DEFAULT NULL,
  `password` varchar(255) NOT NULL,
  `name` varchar(100) DEFAULT NULL,
  `user_token` varchar(100) DEFAULT NULL,
  `acc_lock` int(11) DEFAULT 0,
  `acc_ban` varchar(10) DEFAULT 'off',
  `otp` int(11) DEFAULT NULL,
  `otp_attempts` int(11) DEFAULT 3,
  `blocked_until` varchar(50) DEFAULT NULL,
  `pg_mode` int(11) DEFAULT 1,
  `two_factor` int(11) DEFAULT 0,
  `role` varchar(50) DEFAULT 'User',
  `status` varchar(20) DEFAULT 'Active',
  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `mobile` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";

if ($conn->query($create)) {
    echo "✓ Table created successfully<br><br>";
    
    echo "<h3>Step 3: Creating a test user...</h3>";
    // Create a test user: mobile=1234567890, password=password
    $hashedPassword = password_hash('password', PASSWORD_DEFAULT);
    $insert = "INSERT INTO users (mobile, email, password, name, role) VALUES 
               ('1234567890', 'test@example.com', '$hashedPassword', 'Test User', 'User')";
    
    if ($conn->query($insert)) {
        echo "✓ Test user created<br>";
        echo "Mobile: 1234567890<br>";
        echo "Password: password<br><br>";
        echo "<strong>You can now login with these credentials!</strong>";
    } else {
        echo "✗ Error creating test user: " . $conn->error;
    }
} else {
    echo "✗ Error creating table: " . $conn->error;
}
?>
