<?php
// UHD-Lite Installation Wizard
session_start();
$step = isset($_GET['step']) ? (int)$_GET['step'] : 1;
$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && $step == 2) {
    $db_host = $_POST['db_host'];
    $db_name = $_POST['db_name'];
    $db_user = $_POST['db_user'];
    $db_pass = $_POST['db_pass'];
    $admin_email = $_POST['admin_email'];
    $admin_password = password_hash($_POST['admin_password'], PASSWORD_DEFAULT);
    
    $conn = @new mysqli($db_host, $db_user, $db_pass);
    if ($conn->connect_error) {
        $error = "Database connection failed: " . $conn->connect_error;
    } else {
        $conn->query("CREATE DATABASE IF NOT EXISTS `$db_name`");
        $conn->select_db($db_name);
        
        // Read and execute setup.sql
        $sql = file_get_contents(__DIR__ . '/install/setup.sql');
        $conn->multi_query($sql);
        while ($conn->next_result()) {;}
        
        // Create config file
        $config = "<?php\n";
        $config .= "define('DB_HOST', '$db_host');\n";
        $config .= "define('DB_NAME', '$db_name');\n";
        $config .= "define('DB_USER', '$db_user');\n";
        $config .= "define('DB_PASS', '$db_pass');\n";
        $config .= "?>";
        file_put_contents(__DIR__ . '/config.php', $config);
        
        $success = "Installation complete! You can now use the system.";
        echo "<script>setTimeout(function(){ window.location.href='dashboard.php'; }, 2000);</script>";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>UHD-Lite Installer</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container mt-5">
    <div class="card mx-auto" style="max-width: 600px;">
        <div class="card-header bg-success text-white text-center">
            <h4>UHD-Lite Installation</h4>
        </div>
        <div class="card-body">
            <?php if($error): ?>
                <div class="alert alert-danger"><?= $error ?></div>
            <?php endif; ?>
            <?php if($success): ?>
                <div class="alert alert-success"><?= $success ?></div>
            <?php endif; ?>
            
            <?php if($step == 1): ?>
                <h5>Welcome to UHD-Lite</h5>
                <p>This installer will set up the hospital management system on your server.</p>
                <p><strong>Requirements:</strong></p>
                <ul>
                    <li>PHP 7.4 or higher</li>
                    <li>MySQL 5.7 or higher</li>
                </ul>
                <a href="?step=2" class="btn btn-primary w-100">Start Installation →</a>
            <?php endif; ?>
            
            <?php if($step == 2): ?>
                <form method="POST">
                    <div class="mb-2"><label>Database Host</label><input type="text" name="db_host" class="form-control" value="localhost" required></div>
                    <div class="mb-2"><label>Database Name</label><input type="text" name="db_name" class="form-control" required></div>
                    <div class="mb-2"><label>Database User</label><input type="text" name="db_user" class="form-control" required></div>
                    <div class="mb-2"><label>Database Password</label><input type="password" name="db_pass" class="form-control"></div>
                    <div class="mb-2"><label>Admin Email</label><input type="email" name="admin_email" class="form-control" required></div>
                    <div class="mb-2"><label>Admin Password</label><input type="password" name="admin_password" class="form-control" required></div>
                    <button type="submit" class="btn btn-success w-100">Install</button>
                </form>
            <?php endif; ?>
        </div>
    </div>
</div>
</body>
</html>