<?php
// reset-admin.php - Run this to fix admin login
// DELETE AFTER USE!

require_once 'includes/config.php';

echo "<h2>Admin Password Reset</h2>";

// Create new password hash
$new_password = 'admin123';
$hashed_password = password_hash($new_password, PASSWORD_DEFAULT);

echo "<p>New hash created: " . $hashed_password . "</p>";

// Update admin user
try {
    // First check if admin exists
    $stmt = $pdo->query("SELECT id, email, full_name FROM users WHERE role = 'admin' LIMIT 1");
    $admin = $stmt->fetch();
    
    if($admin) {
        // Update password
        $stmt = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
        $stmt->execute([$hashed_password, $admin['id']]);
        
        echo "<p style='color:green;'>✅ Admin password updated!</p>";
        echo "<p>Email: " . $admin['email'] . "</p>";
        echo "<p>Password: admin123</p>";
    } else {
        // Create admin user if not exists
        $stmt = $pdo->prepare("INSERT INTO users (full_name, email, password, role, phone_verified) VALUES (?, ?, ?, 'admin', 1)");
        $stmt->execute(['Admin', 'admin@lms.com', $hashed_password]);
        
        echo "<p style='color:green;'>✅ Admin user created!</p>";
        echo "<p>Email: admin@lms.com</p>";
        echo "<p>Password: admin123</p>";
    }
    
    // Also create a test student user
    $stmt = $pdo->prepare("INSERT IGNORE INTO users (full_name, email, password, role, phone_verified) VALUES (?, ?, ?, 'student', 1)");
    $stmt->execute(['Test Student', 'student@lms.com', $hashed_password]);
    
    echo "<hr>";
    echo "<p><strong>Test Accounts:</strong></p>";
    echo "<p>Admin: admin@lms.com / admin123</p>";
    echo "<p>Student: student@lms.com / admin123</p>";
    echo "<hr>";
    echo "<p><a href='auth.php?mode=login' style='background:#6C5CE7;color:white;padding:10px 20px;text-decoration:none;border-radius:5px;'>Go to Login Page</a></p>";
    echo "<p style='color:red;'>⚠️ DELETE THIS FILE NOW!</p>";
    
} catch(PDOException $e) {
    echo "<p style='color:red;'>Error: " . $e->getMessage() . "</p>";
}
?>