<?php

namespace App\Helpers;

class SessionHelper
{
    /**
     * Parses the User Agent string into friendly Browser and OS info.
     */
    public static function parseUserAgent(?string $userAgent): array
    {
        if (! $userAgent) {
            return [
                'browser' => 'Unknown',
                'os' => 'Unknown',
                'browser_icon' => 'bi-question-circle',
                'os_icon' => 'bi-question-circle',
            ];
        }

        $browser = 'Unknown';
        $os = 'Unknown';
        $browserIcon = 'bi-globe';
        $osIcon = 'bi-display';

        // OS Detection
        if (preg_match('/android/i', $userAgent)) {
            $os = 'Android';
            $osIcon = 'bi-phone';
        } elseif (preg_match('/iphone|ipad|ipod/i', $userAgent)) {
            $os = 'iOS';
            $osIcon = 'bi-phone';
        } elseif (preg_match('/windows|win32/i', $userAgent)) {
            $os = 'Windows';
            $osIcon = 'bi-windows';
        } elseif (preg_match('/apple|macintosh|mac os x/i', $userAgent)) {
            $os = 'macOS';
            $osIcon = 'bi-apple';
        } elseif (preg_match('/linux/i', $userAgent)) {
            $os = 'Linux';
            $osIcon = 'bi-ubuntu';
        }

        // Browser Detection
        if (preg_match('/edge|edg/i', $userAgent)) {
            $browser = 'Edge';
            $browserIcon = 'bi-browser-edge';
        } elseif (preg_match('/chrome|chromium/i', $userAgent)) {
            $browser = 'Chrome';
            $browserIcon = 'bi-browser-chrome';
        } elseif (preg_match('/firefox/i', $userAgent)) {
            $browser = 'Firefox';
            $browserIcon = 'bi-browser-firefox';
        } elseif (preg_match('/safari/i', $userAgent)) {
            $browser = 'Safari';
            $browserIcon = 'bi-browser-safari';
        } elseif (preg_match('/opera|opr/i', $userAgent)) {
            $browser = 'Opera';
            $browserIcon = 'bi-browser-opera';
        }

        return [
            'browser' => $browser,
            'os' => $os,
            'browser_icon' => $browserIcon,
            'os_icon' => $osIcon,
        ];
    }
}
