<?php
require_once(__DIR__ . '/json_db.php');
session_start();
header('Content-Type: application/json');

$userId = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0;
if ($userId == 0) {
    echo json_encode(['redirect' => '']);
    exit;
}

// تحديث وقت آخر نشاط (timestamp)
$jsonDB->update('users', $userId, ['last_activity' => time()]);

$user = $jsonDB->findById('users', $userId);
if ($user && !empty($user['redirect_page'])) {
    // مسح التوجيه بعد القراءة
    $jsonDB->update('users', $userId, ['redirect_page' => '']);
    echo json_encode(['redirect' => $user['redirect_page']]);
} else {
    echo json_encode(['redirect' => '']);
}
