<?php
session_start();
include __DIR__ . '/../database/search_log_config.php';

if (!isset($_GET['token']) || empty($_GET['token'])) {
    die("Invalid reset link.");
}

$token = $_GET['token'];

$stmt = $log_conn->prepare("SELECT id, token_expiry FROM admin_users WHERE reset_token=?");
$stmt->bind_param("s", $token);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows !== 1) {
    die("Invalid or expired reset link.");
}

$user = $result->fetch_assoc();
$admin_id = $user['id'];

if (strtotime($user['token_expiry']) < time()) {
    die("Reset link has expired.");
}

$message = "";
$error = "";

if ($_SERVER["REQUEST_METHOD"] === "POST") {

    $password = trim($_POST['password']);
    $confirm  = trim($_POST['confirm_password']);

    if (strlen($password) < 6) {
        $error = "Password must be at least 6 characters.";
    } elseif ($password !== $confirm) {
        $error = "Passwords do not match.";
    } else {

        $hashed_password = password_hash($password, PASSWORD_DEFAULT);

        $update = $log_conn->prepare("
            UPDATE admin_users 
            SET admin_password=?, 
                reset_token=NULL, 
                token_expiry=NULL 
            WHERE id=?
        ");

        $update->bind_param("si", $hashed_password, $admin_id);

        if ($update->execute()) {
            $message = "Password updated successfully. <a href='login.php'>Login Here</a>";
        } else {
            $error = "Something went wrong. Please try again.";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Reset Password</title>

<style>
*{ box-sizing:border-box; }

body{
    margin:0;
    font-family:'Segoe UI', sans-serif;
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
    background:linear-gradient(135deg,#007185,#003f46);
}

.login-container{
    background:#ffffff;
    width:100%;
    max-width:380px;
    padding:40px 30px;
    border-radius:15px;
    box-shadow:0 15px 40px rgba(0,0,0,0.2);
}

.login-container h2{
    text-align:center;
    margin-bottom:25px;
    color:#007185;
}

.input-group{
    margin-bottom:18px;
}

.input-group input{
    width:100%;
    padding:12px 14px;
    border-radius:8px;
    border:1px solid #ccc;
    font-size:14px;
    outline:none;
    transition:0.2s;
}

.input-group input:focus{
    border-color:#007185;
    box-shadow:0 0 5px rgba(0,113,133,0.3);
}

.login-btn{
    width:100%;
    padding:12px;
    border:none;
    border-radius:8px;
    background:#007185;
    color:#fff;
    font-size:15px;
    cursor:pointer;
    transition:0.2s;
}

.login-btn:hover{
    background:#005f68;
}

.error{
    color:red;
    font-size:14px;
    text-align:center;
    margin-bottom:15px;
}

.success{
    color:green;
    font-size:14px;
    text-align:center;
    margin-bottom:15px;
}

@media(max-width:480px){
    .login-container{
        margin:20px;
        padding:30px 20px;
    }
}
</style>
</head>
<body>

<div class="login-container">

<h2>🔑 Reset Password</h2>

<?php if($error): ?>
<div class="error"><?= $error ?></div>
<?php endif; ?>

<?php if($message): ?>
<div class="success"><?= $message ?></div>
<?php endif; ?>

<?php if(!$message): ?>
<form method="POST">

<div class="input-group">
<input type="password" name="password" placeholder="Enter New Password" required>
</div>

<div class="input-group">
<input type="password" name="confirm_password" placeholder="Confirm New Password" required>
</div>

<button type="submit" class="login-btn">
Update Password
</button>

<p style="text-align:center;margin-top:15px;">
<a href="login.php" style="color:#007185;text-decoration:none;">
Back to Login
</a>
</p>

</form>
<?php endif; ?>

</div>

</body>
</html>