<?php
// Password testing and generation script for ENT Hospital System

echo "=== ENT Hospital Password Hash Testing ===\n\n";

// The hash from your database schema (actual admin user hash)
$db_hash = '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi';
echo "Database hash: " . $db_hash . "\n\n";

// Common test passwords
$test_passwords = ['password', 'admin', '123456', 'admin123', 'hospital', 'ent123', 'welcome'];

echo "Testing common passwords against database hash:\n";
foreach ($test_passwords as $test_password) {
    $result = password_verify($test_password, $db_hash);
    echo sprintf("%-12s: %s\n", $test_password, $result ? 'SUCCESS ✓' : 'FAILED ✗');
}

echo "\n" . str_repeat("=", 50) . "\n";
echo "GENERATING NEW PASSWORD HASHES\n";
echo str_repeat("=", 50) . "\n\n";

// Generate new hashes for common passwords
$passwords_to_hash = [
    'admin' => 'Default admin password',
    '123456' => 'Simple numeric password',
    'hospital123' => 'Hospital-themed password',
    'ent2024' => 'ENT system password',
    'Welcome@123' => 'Strong password example'
];

foreach ($passwords_to_hash as $password => $description) {
    $hash = password_hash($password, PASSWORD_DEFAULT);
    echo "Password: '$password' ($description)\n";
    echo "Hash: $hash\n";
    echo "Verification: " . (password_verify($password, $hash) ? 'SUCCESS ✓' : 'FAILED ✗') . "\n\n";
}

echo str_repeat("=", 50) . "\n";
echo "UPDATE SQL COMMANDS\n";
echo str_repeat("=", 50) . "\n\n";

// Generate SQL update commands
$recommended_passwords = [
    'admin' => password_hash('admin', PASSWORD_DEFAULT),
    'hospital123' => password_hash('hospital123', PASSWORD_DEFAULT)
];

echo "-- Update admin user password in your database:\n\n";

foreach ($recommended_passwords as $plain_password => $hashed_password) {
    echo "-- For password: '$plain_password'\n";
    echo "UPDATE users SET password_hash = '$hashed_password' WHERE username = 'admin';\n\n";
}

echo str_repeat("=", 50) . "\n";
echo "SECURITY RECOMMENDATIONS\n";
echo str_repeat("=", 50) . "\n\n";

echo "1. Use strong passwords (at least 8 characters with mixed case, numbers, symbols)\n";
echo "2. Change default passwords immediately after system setup\n";
echo "3. Implement password complexity requirements in your system\n";
echo "4. Consider implementing password expiration policies\n";
echo "5. Use two-factor authentication for administrative accounts\n\n";

echo "=== Test Complete ===\n";

// Additional function to test a specific password
function test_specific_password($password, $hash) {
    echo "\nTesting specific password:\n";
    echo "Password: '$password'\n";
    echo "Hash: $hash\n";
    echo "Result: " . (password_verify($password, $hash) ? 'MATCH ✓' : 'NO MATCH ✗') . "\n";
}

// Test the hash you were using in your script
echo "\n" . str_repeat("-", 50) . "\n";
echo "TESTING YOUR ORIGINAL HASH\n";
echo str_repeat("-", 50) . "\n";

$your_test_hash = '$2y$10$fCrWvH3/4vU1KpyfGZiOOeUu1q5k7jhVz7A5.OmXzP4wKJ5.ZfiGi';
echo "Your test hash: $your_test_hash\n\n";

echo "Testing common passwords against your hash:\n";
foreach ($test_passwords as $test_password) {
    $result = password_verify($test_password, $your_test_hash);
    echo sprintf("%-12s: %s\n", $test_password, $result ? 'SUCCESS ✓' : 'FAILED ✗');
}

echo "\n=== RECOMMENDATION ===\n";
echo "Since neither hash matches common passwords, you should:\n";
echo "1. Generate a new hash for a known password\n";
echo "2. Update your database with the new hash\n";
echo "3. Use the known password to test login\n\n";

// Generate a fresh hash for 'admin'
$fresh_admin_hash = password_hash('admin', PASSWORD_DEFAULT);
echo "Fresh hash for 'admin' password:\n";
echo "$fresh_admin_hash\n\n";

echo "SQL to update:\n";
echo "UPDATE users SET password_hash = '$fresh_admin_hash' WHERE user_id = 1;\n";
?>