<?php
require_once __DIR__ . '/../config/auth.php';

if (isLoggedIn()) {
    header('Location: ' . getRedirectUrl());
    exit;
}

$error = '';
$message = '';
$token = $_GET['token'] ?? '';
$valid_token = false;
$user_id = null;

if ($token) {
    $pdo = getDB();
    $stmt = $pdo->prepare("SELECT id FROM users WHERE reset_token = ? AND reset_token_expires > NOW() AND status = 1");
    $stmt->execute([$token]);
    $user = $stmt->fetch();
    if ($user) {
        $valid_token = true;
        $user_id = $user['id'];
    } else {
        $error = 'Invalid or expired reset token.';
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && $valid_token) {
    $password = $_POST['password'] ?? '';
    $confirm = $_POST['confirm_password'] ?? '';

    if (strlen($password) < 6) {
        $error = 'Password must be at least 6 characters.';
    } elseif ($password !== $confirm) {
        $error = 'Passwords do not match.';
    } else {
        $hash = password_hash($password, PASSWORD_DEFAULT);
        $stmt = $pdo->prepare("UPDATE users SET password = ?, reset_token = NULL, reset_token_expires = NULL WHERE id = ?");
        $stmt->execute([$hash, $user_id]);
        logActivity('Password Reset', 'Auth', $user_id);
        $message = 'Password reset successful. <a href="' . BASE_URL . 'auth/login.php">Login now</a>.';
        $valid_token = false;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password - DigiMobiz</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="<?= BASE_URL ?>assets/css/style.css" rel="stylesheet">
</head>
<body class="bg-light">
    <div class="container">
        <div class="row justify-content-center align-items-center min-vh-100">
            <div class="col-md-4">
                <div class="card shadow-lg border-0 rounded-3">
                    <div class="card-header bg-info text-white text-center py-4 rounded-top">
                        <i class="fas fa-key fa-3x mb-2"></i>
                        <h4 class="mb-0">Reset Password</h4>
                        <small>Enter your new password</small>
                    </div>
                    <div class="card-body p-4">
                        <?php if ($message): ?>
                            <div class="alert alert-success"><?= $message ?></div>
                        <?php endif; ?>
                        <?php if ($error): ?>
                            <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
                        <?php endif; ?>
                        <?php if ($valid_token): ?>
                            <form method="POST">
                                <div class="mb-3">
                                    <label class="form-label"><i class="fas fa-lock me-2"></i>New Password</label>
                                    <input type="password" name="password" class="form-control form-control-lg" placeholder="Min 6 characters" required minlength="6">
                                </div>
                                <div class="mb-3">
                                    <label class="form-label"><i class="fas fa-check-circle me-2"></i>Confirm Password</label>
                                    <input type="password" name="confirm_password" class="form-control form-control-lg" placeholder="Re-enter password" required>
                                </div>
                                <div class="d-grid">
                                    <button type="submit" class="btn btn-info btn-lg text-white"><i class="fas fa-save me-2"></i>Reset Password</button>
                                </div>
                            </form>
                        <?php endif; ?>
                        <div class="text-center mt-3">
                            <a href="<?= BASE_URL ?>auth/login.php" class="text-muted small"><i class="fas fa-arrow-left me-1"></i>Back to Login</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
