<?php
session_start();
require_once "config/database.php";

if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    header("Location: login.php");
    exit;
}

$database = new Database();
$db = $database->getConnection();

if (!$db) {
    die("Database connection failed.");
}

$message = "";
$message_color = "green";

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid user ID.");
}

$user_id = (int)$_GET['id'];

$stmt = $db->prepare("
    SELECT id, full_name, username, email, role, status
    FROM users
    WHERE id = :id
    LIMIT 1
");
$stmt->bindValue(":id", $user_id, PDO::PARAM_INT);
$stmt->execute();

$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    die("User not found.");
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $new_password = trim($_POST['new_password'] ?? '');
    $confirm_password = trim($_POST['confirm_password'] ?? '');

    if ($new_password === '' || $confirm_password === '') {
        $message = "Please fill all fields.";
        $message_color = "red";
    } elseif (strlen($new_password) < 6) {
        $message = "Password must be at least 6 characters.";
        $message_color = "red";
    } elseif ($new_password !== $confirm_password) {
        $message = "Passwords do not match.";
        $message_color = "red";
    } else {
        $hashed_password = password_hash($new_password, PASSWORD_DEFAULT);

        $update = $db->prepare("
            UPDATE users
            SET password = :password
            WHERE id = :id
            LIMIT 1
        ");
        $update->bindValue(":password", $hashed_password);
        $update->bindValue(":id", $user_id, PDO::PARAM_INT);

        if ($update->execute()) {
            $message = "Password reset successfully.";
            $message_color = "green";
        } else {
            $message = "Failed to reset password.";
            $message_color = "red";
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset User Password</title>
    <link rel="stylesheet" href="assets/css/app.css">
</head>
<body>
<div class="layout">
    <?php include 'nav.php'; ?>

    <main class="main">
        <div class="hero">
            <div class="hero-top">
                <div>
                    <h1>Reset Password</h1>
                    <p>Admin only page for resetting a user's password.</p>
                </div>
                <div class="welcome-badge">
                    Admin
                </div>
            </div>
        </div>

        <?php if ($message !== ''): ?>
            <div class="alert-box <?php echo $message_color === 'green' ? 'alert-success' : 'alert-danger'; ?>">
                <?php echo htmlspecialchars($message); ?>
            </div>
        <?php endif; ?>

        <div class="card">
            <div class="section-title">User Information</div>
            <div class="section-subtext">You are resetting password for this account.</div>

            <div class="form-grid">
                <div class="form-group">
                    <label>Full Name</label>
                    <input type="text" value="<?php echo htmlspecialchars($user['full_name'] ?? ''); ?>" disabled>
                </div>

                <div class="form-group">
                    <label>Username</label>
                    <input type="text" value="<?php echo htmlspecialchars($user['username'] ?? ''); ?>" disabled>
                </div>

                <div class="form-group">
                    <label>Email</label>
                    <input type="text" value="<?php echo htmlspecialchars($user['email'] ?? ''); ?>" disabled>
                </div>

                <div class="form-group">
                    <label>Role</label>
                    <input type="text" value="<?php echo htmlspecialchars($user['role'] ?? ''); ?>" disabled>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="section-title">Set New Password</div>
            <div class="section-subtext">Enter a temporary password for this user.</div>

            <form method="POST">
                <div class="form-grid">
                    <div class="form-group">
                        <label for="new_password">New Password</label>
                        <input type="password" name="new_password" id="new_password" required>
                    </div>

                    <div class="form-group">
                        <label for="confirm_password">Confirm Password</label>
                        <input type="password" name="confirm_password" id="confirm_password" required>
                    </div>
                </div>

                <div class="page-actions">
                    <button type="submit" class="btn btn-primary">Reset Password</button>
                    <a href="users.php" class="btn btn-secondary">Back to Users</a>
                </div>
            </form>
        </div>
    </main>
</div>
</body>
</html>