<?php
declare(strict_types=1);

require_once dirname(__DIR__) . '/_bootstrap.php';
require_once dirname(__DIR__, 2) . '/common/web_session_auth.php';

golder_require_method('POST');

$master = golder_master_pdo();
$ctx = golder_require_current_web_customer_session($master);
$input = golder_api_input();

$sessionId = trim((string)($input['sessionId'] ?? $input['session_id'] ?? ''));
$tabId = trim((string)($input['tabId'] ?? $input['tab_id'] ?? ''));

if ($sessionId === '') {
    golder_json_error(400, 'INVALID_REQUEST', 'sessionId is required.');
}
if (!hash_equals($ctx['session_uuid'], $sessionId)) {
    golder_json_error(403, 'SESSION_MISMATCH', 'Session does not match current login.');
}

if ($tabId !== '') {
    golder_mark_web_session_tab_disconnected($master, $ctx['session_uuid'], $tabId);
}

golder_json_response([
    'status' => 'ok',
    'session' => [
        'id' => $ctx['session_uuid'],
        'active_tabs' => golder_count_active_web_session_tabs($master, $ctx['session_uuid']),
    ],
]);


