<?php
// fix-database.php
// Run this ONCE to fix database, then delete it

require_once 'includes/config.php';

echo "<h1>Database Fix</h1>";

// Add phone_verified column
try {
    $pdo->exec("ALTER TABLE users ADD COLUMN phone_verified TINYINT(1) DEFAULT 0");
    echo "<p style='color:green'>✅ Added phone_verified column</p>";
} catch(PDOException $e) {
    if(strpos($e->getMessage(), 'Duplicate') !== false) {
        echo "<p style='color:blue'>Column already exists</p>";
    } else {
        echo "<p style='color:red'>Error: " . $e->getMessage() . "</p>";
    }
}

// Add otp_code column
try {
    $pdo->exec("ALTER TABLE users ADD COLUMN otp_code VARCHAR(6)");
    echo "<p style='color:green'>✅ Added otp_code column</p>";
} catch(PDOException $e) {
    echo "<p style='color:blue'>May already exist</p>";
}

// Add otp_expiry column
try {
    $pdo->exec("ALTER TABLE users ADD COLUMN otp_expiry DATETIME");
    echo "<p style='color:green'>✅ Added otp_expiry column</p>";
} catch(PDOException $e) {
    echo "<p style='color:blue'>May already exist</p>";
}

// Update admin password
$newpass = password_hash('admin123', PASSWORD_DEFAULT);
$pdo->exec("UPDATE users SET password = '$newpass', phone_verified = 1 WHERE email = 'admin@banglalms.com' OR email = 'admin@lms.com'");
echo "<p style='color:green'>✅ Admin password updated to: admin123</p>";

echo "<hr>";
echo "<h2>Done!</h2>";
echo "<p><a href='simple-login.php'>Login Here</a></p>";
echo "<p>Email: admin@banglalms.com<br>Password: admin123</p>";
echo "<p style='color:red'>Delete this file now!</p>";
?>