<?php
/**
 * Database Configuration Fixer
 * Updates database.php with correct credentials
 */

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $new_password = $_POST['password'] ?? '';
    
    // Read current database.php
    $config_file = __DIR__ . '/config/database.php';
    $content = file_get_contents($config_file);
    
    if ($content === false) {
        die("Could not read database.php file");
    }
    
    // Update password line
    $pattern = '/\$password\s*=\s*[\'"][^\'"]*[\'"];/';
    $replacement = "\$password = '" . addslashes($new_password) . "';";
    
    $new_content = preg_replace($pattern, $replacement, $content);
    
    // Test the new configuration
    try {
        $host = 'localhost';
        $dbname = 'school_management';
        $username = 'root';
        
        $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $new_password, [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES => false,
        ]);
        
        // If connection successful, save the file
        if (file_put_contents($config_file, $new_content) !== false) {
            echo "<div style='background: #d4edda; padding: 15px; border: 1px solid #c3e6cb; border-radius: 5px;'>";
            echo "<h3 style='color: #155724;'>✅ Success!</h3>";
            echo "<p style='color: #155724;'>Database configuration updated successfully.</p>";
            echo "<p><strong>New password:</strong> " . ($new_password === '' ? '(empty)' : htmlspecialchars($new_password)) . "</p>";
            echo "<p><a href='test_db_connection.php'>Test connection again</a></p>";
            echo "</div>";
        } else {
            echo "<div style='background: #f8d7da; padding: 15px; border: 1px solid #f5c6cb; border-radius: 5px;'>";
            echo "<h3 style='color: #721c24;'>❌ Error</h3>";
            echo "<p style='color: #721c24;'>Could not write to database.php file. Please check file permissions.</p>";
            echo "</div>";
        }
        
    } catch (PDOException $e) {
        echo "<div style='background: #f8d7da; padding: 15px; border: 1px solid #f5c6cb; border-radius: 5px;'>";
        echo "<h3 style='color: #721c24;'>❌ Connection Failed</h3>";
        echo "<p style='color: #721c24;'>Could not connect with this password: " . $e->getMessage() . "</p>";
        echo "</div>";
    }
    
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Database Configuration Fixer</title>
    <meta charset="UTF-8">
    <style>
        body { font-family: Arial, sans-serif; max-width: 600px; margin: 50px auto; padding: 20px; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; font-weight: bold; }
        input[type="password"] { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; }
        button { background: #007cba; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; }
        button:hover { background: #005a87; }
        .instructions { background: #e9ecef; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
    </style>
</head>
<body>
    <h2>Database Configuration Fixer</h2>
    
    <div class="instructions">
        <h3>Instructions:</h3>
        <p>1. First, run <a href="test_db_connection.php">test_db_connection.php</a> to find your correct MySQL password</p>
        <p>2. Enter the working password below</p>
        <p>3. This will automatically update your database configuration</p>
    </div>
    
    <form method="POST">
        <div class="form-group">
            <label for="password">MySQL Password:</label>
            <input type="password" id="password" name="password" placeholder="Enter your MySQL password" required>
        </div>
        
        <button type="submit">Update Database Configuration</button>
    </form>
    
    <div style="margin-top: 30px; padding: 15px; background: #fff3cd; border: 1px solid #ffeaa7; border-radius: 5px;">
        <h3>Alternative Manual Fix:</h3>
        <p>Edit <code>config/database.php</code> line 6:</p>
        <pre>$password = 'your_actual_password';</pre>
    </div>
</body>
</html>