<?php
if (session_status() === PHP_SESSION_NONE) { session_start(); }
if (!defined('BASE_PATH')) { define('BASE_PATH', dirname(__DIR__)); }
require_once BASE_PATH . '/database/config.php';
require_once BASE_PATH . '/database/connection.php';
header('Content-Type: application/json; charset=utf-8');

$cid = (int)($_SESSION['client_id'] ?? 0);
if ($cid <= 0) { echo json_encode(['success'=>false]); exit; }
try {
  $db = Database::getInstance();
  $pdo = $db->getConnection();
  try { $pdo->query("SHOW COLUMNS FROM clients LIKE 'last_seen'"); } catch (Throwable $__) { $pdo->exec("ALTER TABLE clients ADD COLUMN last_seen DATETIME NULL"); }
  $stmt = $pdo->prepare('UPDATE clients SET last_seen = NOW() WHERE id = ?');
  $stmt->execute([$cid]);
  echo json_encode(['success'=>true]);
} catch (Throwable $e) { echo json_encode(['success'=>false]); }
?>
