<?php

use App\Helpers\SessionHelper;

test('parseUserAgent returns Unknown for null', function () {
    $result = SessionHelper::parseUserAgent(null);

    expect($result['browser'])->toBe('Unknown');
    expect($result['os'])->toBe('Unknown');
    expect($result['browser_icon'])->toBe('bi-question-circle');
    expect($result['os_icon'])->toBe('bi-question-circle');
});

test('parseUserAgent returns Unknown for empty string', function () {
    expect(SessionHelper::parseUserAgent('')['browser'])->toBe('Unknown');
});

test('parseUserAgent detects Android', function () {
    $ua = 'Mozilla/5.0 (Linux; Android 14; Pixel 8) Chrome/120.0.0.0 Mobile Safari/537.36';
    $r = SessionHelper::parseUserAgent($ua);
    expect($r['os'])->toBe('Android');
    expect($r['os_icon'])->toBe('bi-phone');
});

test('parseUserAgent detects iOS', function () {
    $ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15';
    expect(SessionHelper::parseUserAgent($ua)['os'])->toBe('iOS');
});

test('parseUserAgent detects Windows', function () {
    $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0';
    $r = SessionHelper::parseUserAgent($ua);
    expect($r['os'])->toBe('Windows');
    expect($r['os_icon'])->toBe('bi-windows');
});

test('parseUserAgent detects macOS', function () {
    $ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15';
    $r = SessionHelper::parseUserAgent($ua);
    expect($r['os'])->toBe('macOS');
    expect($r['os_icon'])->toBe('bi-apple');
});

test('parseUserAgent detects Linux desktop', function () {
    $ua = 'Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/121.0';
    $r = SessionHelper::parseUserAgent($ua);
    expect($r['os'])->toBe('Linux');
});

test('parseUserAgent detects Edge before Chrome', function () {
    $ua = 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 Chrome/120 Safari/537.36 Edg/120.0';
    expect(SessionHelper::parseUserAgent($ua)['browser'])->toBe('Edge');
});

test('parseUserAgent detects Chrome', function () {
    $ua = 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 Chrome/120.0 Safari/537.36';
    expect(SessionHelper::parseUserAgent($ua)['browser'])->toBe('Chrome');
});

test('parseUserAgent detects Firefox', function () {
    $ua = 'Mozilla/5.0 (Windows NT 10.0; rv:121.0) Gecko/20100101 Firefox/121.0';
    expect(SessionHelper::parseUserAgent($ua)['browser'])->toBe('Firefox');
});

test('parseUserAgent detects Safari (without Chrome)', function () {
    $ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 Version/17.0 Safari/605.1.15';
    expect(SessionHelper::parseUserAgent($ua)['browser'])->toBe('Safari');
});

test('parseUserAgent falls back to bi-globe icon when browser unknown', function () {
    $ua = 'Some-Strange-Bot/1.0';
    $r = SessionHelper::parseUserAgent($ua);
    expect($r['browser'])->toBe('Unknown');
    expect($r['browser_icon'])->toBe('bi-globe');
});
