<?php
require_once('public/connection.php'); // Adjust path if needed
	ini_set('display_errors', 1);
error_reporting(E_ALL);
// 1. Hash Admin Passwords
$admin_query = "SELECT id, password FROM users";
$admin_result = mysqli_query($connection, $admin_query);
while ($admin = mysqli_fetch_assoc($admin_result)) {
    $id = $admin['id'];
    $plain_password = $admin['password'];
    $hashed = password_hash($plain_password, PASSWORD_DEFAULT);
    mysqli_query($connection, "UPDATE users SET password = '$hashed' WHERE id = $id");
}

// 2. Hash Teacher Passwords
$teacher_query = "SELECT id, password FROM teachers";
$teacher_result = mysqli_query($connection, $teacher_query);
while ($teacher = mysqli_fetch_assoc($teacher_result)) {
    $id = $teacher['id'];
    $plain_password = $teacher['password'];
    $hashed = password_hash($plain_password, PASSWORD_DEFAULT);
    mysqli_query($connection, "UPDATE teachers SET password = '$hashed' WHERE id = $id");
}

// 3. Hash Student Passwords
$student_query = "SELECT id, gen_password FROM students";
$student_result = mysqli_query($connection, $student_query);
while ($student = mysqli_fetch_assoc($student_result)) {
    $id = $student['id'];
    $plain_password = $student['gen_password'];
    $hashed = password_hash($plain_password, PASSWORD_DEFAULT);
    mysqli_query($connection, "UPDATE students SET gen_password = '$hashed' WHERE id = $id");
}

echo "All passwords have been hashed successfully.";
?>

