<?php

app_require('classes/system_session_activity_schema_bootstrap.php');
app_require('classes/system_session_activity_tracker.php');

use classes\system_session_activity_tracker;

it('detects device types from common user agents', function (): void {
    expect(system_session_activity_tracker::detectDeviceType('Mozilla/5.0 (Windows NT 10.0; Win64; x64)'))->toBe('desktop');
    expect(system_session_activity_tracker::detectDeviceType('Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X)'))->toBe('mobile');
    expect(system_session_activity_tracker::detectDeviceType('Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X)'))->toBe('tablet');
    expect(system_session_activity_tracker::detectDeviceType('curl/8.4.0'))->toBe('bot');
});

it('treats recent sessions as active within the configured activity window', function (): void {
    $now = time();

    expect(system_session_activity_tracker::isActive(date('Y-m-d H:i:s', $now - 60), 15, $now))->toBeTrue();
    expect(system_session_activity_tracker::isActive(date('Y-m-d H:i:s', $now - 1200), 15, $now))->toBeFalse();
    expect(system_session_activity_tracker::isActive(null, 15, $now))->toBeFalse();
});

it('converts database utc datetimes into timezone-aware iso strings', function (): void {
    expect(system_session_activity_tracker::databaseDateTimeToIso8601('2026-04-08 17:39:39'))
        ->toBe('2026-04-08T17:39:39+00:00');

    expect(system_session_activity_tracker::databaseDateTimeToIso8601(null))->toBeNull();
    expect(system_session_activity_tracker::databaseDateTimeToIso8601(''))->toBeNull();
});

it('treats timezone-aware iso timestamps as active using absolute time', function (): void {
    $referenceTimestamp = strtotime('2026-04-08T19:45:00+02:00');

    expect(system_session_activity_tracker::isActive('2026-04-08T17:39:39+00:00', 15, $referenceTimestamp))->toBeTrue();
    expect(system_session_activity_tracker::isActive('2026-04-08T17:20:00+00:00', 15, $referenceTimestamp))->toBeFalse();
});
